feat(test): begin testing visitor
This commit is contained in:
parent
8b027f2321
commit
31d1977f3b
|
@ -76,6 +76,7 @@ const bundle = {
|
||||||
],
|
],
|
||||||
'node_modules/noicejs/out/main-bundle.js': [
|
'node_modules/noicejs/out/main-bundle.js': [
|
||||||
'BaseError',
|
'BaseError',
|
||||||
|
'ConsoleLogger',
|
||||||
],
|
],
|
||||||
'node_modules/js-yaml/index.js': [
|
'node_modules/js-yaml/index.js': [
|
||||||
'DEFAULT_SAFE_SCHEMA',
|
'DEFAULT_SAFE_SCHEMA',
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { Rule, resolveRules, makeSelector } from 'src/rule';
|
import { ConsoleLogger } from 'noicejs';
|
||||||
|
import { mock } from 'sinon';
|
||||||
|
|
||||||
|
import { makeSelector, resolveRules, Rule, visitRules } from 'src/rule';
|
||||||
|
import { VisitorContext } from 'src/visitor/context';
|
||||||
|
|
||||||
const TEST_RULES = [new Rule({
|
const TEST_RULES = [new Rule({
|
||||||
name: 'foo',
|
name: 'foo',
|
||||||
|
@ -94,3 +98,67 @@ describe('rule resolver', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('rule visitor', () => {
|
||||||
|
it('should only call visit for selected items', async () => {
|
||||||
|
const ctx = new VisitorContext({
|
||||||
|
coerce: false,
|
||||||
|
defaults: false,
|
||||||
|
logger: new ConsoleLogger(),
|
||||||
|
});
|
||||||
|
const data = {};
|
||||||
|
const rule = new Rule({
|
||||||
|
name: 'foo',
|
||||||
|
desc: '',
|
||||||
|
level: 'info',
|
||||||
|
tags: [],
|
||||||
|
select: '$',
|
||||||
|
check: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockRule = mock(rule);
|
||||||
|
mockRule.expects('visit').never();
|
||||||
|
|
||||||
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
||||||
|
pickStub.onFirstCall().returns(Promise.resolve([]));
|
||||||
|
pickStub.throws();
|
||||||
|
|
||||||
|
await visitRules(ctx, [rule], {});
|
||||||
|
|
||||||
|
mockRule.verify();
|
||||||
|
expect(ctx.errors.length).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call visit for each selected item', async () => {
|
||||||
|
const ctx = new VisitorContext({
|
||||||
|
coerce: false,
|
||||||
|
defaults: false,
|
||||||
|
logger: new ConsoleLogger(),
|
||||||
|
});
|
||||||
|
const data = {};
|
||||||
|
const rule = new Rule({
|
||||||
|
name: 'foo',
|
||||||
|
desc: '',
|
||||||
|
level: 'info',
|
||||||
|
tags: [],
|
||||||
|
select: '$',
|
||||||
|
check: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockRule = mock(rule);
|
||||||
|
|
||||||
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
||||||
|
pickStub.onFirstCall().returns(Promise.resolve([data]));
|
||||||
|
pickStub.throws();
|
||||||
|
|
||||||
|
const visitStub = mockRule.expects('visit').once().withArgs(ctx, data);
|
||||||
|
visitStub.onFirstCall().returns(Promise.resolve(ctx));
|
||||||
|
visitStub.throws();
|
||||||
|
|
||||||
|
await visitRules(ctx, [rule], {});
|
||||||
|
|
||||||
|
mockRule.verify();
|
||||||
|
expect(ctx.errors.length).to.equal(0);
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue