Skip to content

Commit

Permalink
tests: More test coverage for transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Nefsen402 committed Apr 29, 2024
1 parent 2a9288a commit f2207ed
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 51 deletions.
73 changes: 73 additions & 0 deletions tests/replacement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ test("mount changing text between", () => {
});
});

test("mount changing text between with map", () => {
const elem = document.createElement("body");
const obs = Observer.mutable(0);

mount(elem, h('div', {},
'first',
obs.map(x => x * 2),
'second',
));

obs.set(1);
obs.set(2);

assert.deepEqual(elem.tree(), {
name: 'body',
children: [{
name: 'div',
children: ["first", "4", "second"]
}],
});
});

test("mount changing type", () => {
const elem = document.createElement("body");
const obs = Observer.mutable(0);
Expand Down Expand Up @@ -194,6 +216,40 @@ test("mount div with changing attribute", () => {
});
});

test("mount div with changing attribute with map", () => {
const elem = document.createElement("body");
const o = Observer.mutable(1);

mount(elem, h('div', {val: o.map(o => o * 2)}));
o.set(2);

assert.deepEqual(elem.tree(), {
name: 'body',
children: [{
name: 'div',
attributes: {val: '4'}
}],
});
});

test("mount div with changing attribute with block map", () => {
const elem = document.createElement("body");
const o = Observer.mutable(1);

mount(elem, h('div', {val: o.map(o => {
return o * 2;
})}));
o.set(2);

assert.deepEqual(elem.tree(), {
name: 'body',
children: [{
name: 'div',
attributes: {val: '4'}
}],
});
});

test("mount div with changing property", () => {
const elem = document.createElement("body");
const o = Observer.mutable(1);
Expand All @@ -211,6 +267,23 @@ test("mount div with changing property", () => {
});
});

test("mount div with changing property with map", () => {
const elem = document.createElement("body");
const o = Observer.mutable(1);

mount(elem, h('div', {$val: o.map(o => o * 2)}));

o.set(2);

assert.deepEqual(elem.tree(), {
name: 'body',
children: [{
name: 'div',
val: 4,
}],
});
});

test("mount div with toggle attribute", () => {
const elem = document.createElement("body");
const o = Observer.mutable(true);
Expand Down
102 changes: 51 additions & 51 deletions tests/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,71 @@ import parser from '@babel/parser';
import t from '@babel/types';
import generate from '@babel/generator';


const files = fs.readdirSync(path.resolve(fileURLToPath(import.meta.url), '..'));

for (const file of files) {
if (file === 'transform.test.js') continue;
if (!file.endsWith('.test.js') && !file.endsWith('.test.jsx')) continue;

describe("transform " + file, async () => {
let source = fs.readFileSync(path.resolve(fileURLToPath(import.meta.url), '..', file)).toString();
const ast = parser.parse(source, {
sourceType: 'module',
code: false,
ast: true,
plugins: file.endsWith('.jsx') ? ['jsx'] : [],
});
const transform = (file, options) => async () => {
let source = fs.readFileSync(path.resolve(fileURLToPath(import.meta.url), '..', file)).toString();
const ast = parser.parse(source, {
sourceType: 'module',
code: false,
ast: true,
plugins: file.endsWith('.jsx') ? ['jsx'] : [],
});

htmlLiteral(ast);
htmlLiteral(ast);

if (!file.endsWith('.jsx')) {
staticMount(ast, {
util_import: '..',
});
}
if (!file.endsWith('.jsx')) {
staticMount(ast, options);
}

const context = {};
const context = {};

// replace imports
for (let i = 0; i < ast.program.body.length; i++) {
const child = ast.program.body[i];
if (child.type === 'ImportDeclaration') {
ast.program.body.splice(i--, 1);
// replace imports
for (let i = 0; i < ast.program.body.length; i++) {
const child = ast.program.body[i];
if (child.type === 'ImportDeclaration') {
ast.program.body.splice(i--, 1);

const mod = await import(child.source.value);
for (const spec of child.specifiers) {
if (!spec.imported) {
context[spec.local.name] = mod.default;
} else {
context[spec.local.name] = mod[spec.imported.name];
}
const mod = await import(child.source.value);
for (const spec of child.specifiers) {
if (!spec.imported) {
context[spec.local.name] = mod.default;
} else {
context[spec.local.name] = mod[spec.imported.name];
}
}
}
}

const code = generate.default(ast, {}, source).code;
const code = generate.default(ast, {}, source).code;

for (let o in global) {
if (!(o in context)) context[o] = global[o];
}
for (let o in global) {
if (!(o in context)) context[o] = global[o];
}

if (context.test) context.test = (name, impl) => {
it(name, () => {
try {
return impl();
} catch (e) {
console.log(code.split('\n').map((line, num) => (num + 1) + " " + line).join("\n"));
throw e;
}
});
};

const script = new vm.Script(code, {
filename: file,
if (context.test) context.test = (name, impl) => {
it(name, () => {
try {
return impl();
} catch (e) {
console.log(code.split('\n').map((line, num) => (num + 1) + " " + line).join("\n"));
throw e;
}
});
};

vm.createContext(context);
script.runInContext(context);
const script = new vm.Script(code, {
filename: file,
});

vm.createContext(context);
script.runInContext(context);
}

for (const file of files) {
if (file === 'transform.test.js') continue;
if (!file.endsWith('.test.js') && !file.endsWith('.test.jsx')) continue;

describe("transform with util " + file, transform(file, {util_import: '..'}));
describe("transform " + file, transform(file));
}

0 comments on commit f2207ed

Please sign in to comment.