Add a test for asynchronous visitor

promise-transforms
Kenneth Barbour 2024-02-26 15:20:14 -05:00
parent b7201264a5
commit 6dac05f3ea
2 changed files with 23 additions and 2 deletions

View File

@ -2,6 +2,7 @@ module.exports = {
env: {
node: true,
jest: true,
es6: true,
},
extends: [
"eslint:recommended",

View File

@ -147,14 +147,34 @@ describe('makeVisitor', () => {
});
it('leaves nodes untouched if transformer returns undefined', async () => {
const visitor = makeVisitor([jest.fn(x => undefined)]);
const visitor = makeVisitor([jest.fn(() => undefined)]);
const result = await visit(mockTree(), visitor);
expect(result).toEqual(mockTree());
});
it('throws if a transformer returns strange data', async () => {
const visitor = makeVisitor([jest.fn(x => 'foo')]);
const visitor = makeVisitor([jest.fn(() => 'foo')]);
await expect(visit(mockTree(), visitor))
.rejects.toThrow('Invalid transformer result: "foo"');
});
it('awaits visitor functions', async () => {
// asyncronously add property to element id=foo
const fn = jest.fn(async x => {
if (x?.properties?.id === 'foo') {
await new Promise(resolve => setTimeout(resolve, 10));
return {
...x,
properties: {
...x.properties,
awaited: true,
},
};
}
return x;
});
const visitor = makeVisitor([fn]);
const result = await visit(mockTree(), visitor);
expect(result.children[0].properties.awaited).toBe(true);
});
});