1
0
Fork 0

feat(test): begin testing visitor

This commit is contained in:
ssube 2019-06-30 18:18:11 -05:00
parent 8b027f2321
commit 31d1977f3b
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 70 additions and 1 deletions

View File

@ -76,6 +76,7 @@ const bundle = {
],
'node_modules/noicejs/out/main-bundle.js': [
'BaseError',
'ConsoleLogger',
],
'node_modules/js-yaml/index.js': [
'DEFAULT_SAFE_SCHEMA',

View File

@ -1,5 +1,9 @@
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({
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);
})
});