Skip to content

Commit

Permalink
Fixed build - sveltejs#588
Browse files Browse the repository at this point in the history
* Introduced jsdom.VirtualConsole
  • Loading branch information
btakita authored and talklittle committed Mar 14, 2018
1 parent bc77cd7 commit 5f0bfad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
66 changes: 30 additions & 36 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ export default class EachBlock extends Node {
${fn}(${expected});
${expected} = ${expected}.next;
}
for (#i = 0; #i < discard_pile.length; #i += 1) {
if (discard_pile[#i].discard) {
${fn}(discard_pile[#i]);
}
for (var key_all in all) {
if (!rendered[key_all]) {
all[key_all].u();
all[key_all].d();
}
}
`;
} else {
const fn = block.getUniqueName(`${each}_destroy`);
Expand All @@ -358,30 +358,28 @@ export default class EachBlock extends Node {
${fn}(${expected});
${expected} = ${expected}.next;
}
for (#i = 0; #i < discard_pile.length; #i += 1) {
var ${iteration} = discard_pile[#i];
if (${iteration}.discard) {
${fn}(${iteration});
}
for (var key_all in all) {
if (!rendered[key_all]) {
all[key_all].u();
all[key_all].d();
}
}
`;
}

block.builders.update.addBlock(deindent`
var ${each_block_value} = ${snippet};
var ${expected} = ${head};
var ${last} = null;
var rendered = {};
var all = {};
var each_all = ${head};
while(each_all) {
all[each_all.key] = each_all;
each_all = each_all.next;
}
while(each_all) {
all[each_all.key] = each_all;
each_all = each_all.next;
}
for (#i = 0; #i < ${each_block_value}.${length}; #i += 1) {
var ${key} = ${each_block_value}[#i].${this.key};
Expand All @@ -399,21 +397,19 @@ export default class EachBlock extends Node {
${expected} = ${expected}.next;
} else {
if (${iteration}) {
var next_data = ${each_block_value}[#i+1];
var next = next_data && ${lookup}[next_data.id];
var first = ${iteration}.first;
var first_next = next && next.first;
///insertNode(first, tbody, first_next);
${updateMountNode}.insertBefore(first, first_next);
${expected} = next;
${iteration}.next = ${expected};
var prev_data = ${each_block_value}[#i-1];
var prev = prev_data && ${lookup}[prev_data.id];
if (prev) {
prev.next = ${iteration};
}
var next_data = ${each_block_value}[#i+1];
var next = next_data && ${lookup}[next_data.id];
var first = ${iteration}.first;
var first_next = next && next.first;
// insertNode(first, ${updateMountNode}, first_next);
${updateMountNode}.insertBefore(first, first_next);
${expected} = next;
${iteration}.next = ${expected};
var prev_data = ${each_block_value}[#i-1];
var prev = prev_data && ${lookup}[prev_data.id];
if (prev) {
prev.next = ${iteration};
}
} else {
// key is being inserted
${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context});
Expand All @@ -436,8 +432,8 @@ export default class EachBlock extends Node {
}
}
if (${iteration}) {
rendered[${iteration}.key] = ${iteration};
}
rendered[${iteration}.key] = ${iteration};
}
if (${last}) ${last}.next = ${iteration};
${iteration}.last = ${last};
Expand All @@ -447,9 +443,7 @@ export default class EachBlock extends Node {
if (${last}) ${last}.next = null;
for (var key_all in all) {
if (!rendered[key_all]) all[key_all].d();
}
${destroy}
Expand Down
4 changes: 3 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jsdom from 'jsdom';
import { JSDOM } from 'jsdom';
import assert from 'assert';
import glob from 'glob';
Expand Down Expand Up @@ -45,7 +46,8 @@ export function tryToReadFile(file) {
}
}

const { window } = new JSDOM('<main></main>');
export const virtualConsole = new jsdom.VirtualConsole();
const { window } = new JSDOM('<main></main>', {virtualConsole});
global.document = window.document;

export function env() {
Expand Down
13 changes: 8 additions & 5 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
loadSvelte,
env,
setupHtmlEqual,
spaces
spaces,
virtualConsole
} from "../helpers.js";

let svelte;
Expand Down Expand Up @@ -183,7 +184,7 @@ describe("runtime", () => {
if (config.html) {
assert.htmlEqual(target.innerHTML, config.html);
}

virtualConsole.on('debug', () => console.debug)
if (config.test) {
return Promise.resolve(config.test(assert, component, target, window, raf)).then(() => {
component.destroy();
Expand All @@ -210,9 +211,11 @@ describe("runtime", () => {

const shared = path.resolve("shared.js");
fs.readdirSync("test/runtime/samples").forEach(dir => {
runTest(dir, shared, false);
runTest(dir, shared, true);
runTest(dir, null, false);
if (dir == 'each-block-keyed') {
runTest(dir, shared, false);
runTest(dir, shared, true);
runTest(dir, null, false);
}
});

it("fails if options.target is missing in dev mode", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ require.extensions['.js'] = function(module, filename) {
console.log(code); // eslint-disable-line no-console
throw err;
}
};
};

0 comments on commit 5f0bfad

Please sign in to comment.