silphius/app/astride/evaluators/new.test.js
2024-07-25 09:00:54 -05:00

49 lines
1.1 KiB
JavaScript

import {expect, test} from 'vitest';
import evaluate from '@/astride/evaluate.js';
import expression from '@/astride/test/expression.js';
const scopeTest = test.extend({
scope: async ({}, use) => {
await use({
S: {O: {}},
get(k) { return this.S[k]; },
set(k, v) { return this.S[k] = v; }
});
},
});
class C {
foo = 'bar';
constructor(a, b) {
this.a = a;
this.b = b;
}
}
scopeTest('creates instances', async ({scope}) => {
scope.set('C', C);
const evaluated = evaluate(await expression('new C(1, 2)'), {scope});
expect(evaluated.value)
.to.deep.include({
a: 1,
b: 2,
foo: 'bar',
});
});
scopeTest('creates instances with async dependencies', async ({scope}) => {
scope.set('C', C);
scope.set('a', Promise.resolve(1));
scope.set('b', Promise.resolve(2));
const evaluated = evaluate(await expression('new C(await a, await b)'), {scope});
expect(evaluated.async)
.to.equal(true);
expect(await evaluated.value)
.to.deep.include({
a: 1,
b: 2,
foo: 'bar',
});
});