Skip to content

Commit

Permalink
fix(test): cover session listener, some match and match branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Apr 3, 2020
1 parent ece990c commit 06ffb42
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"typeorm": "0.2.24"
},
"devDependencies": {
"@apextoaster/js-utils": "^0.1.6",
"@apextoaster/js-utils": "^0.1.7",
"@apextoaster/js-yaml-schema": "^0.2.0",
"@istanbuljs/nyc-config-typescript": "1.0.1",
"@microsoft/api-documenter": "7.7.16",
Expand Down
89 changes: 89 additions & 0 deletions test/listener/TestSessionListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect } from 'chai';
import { ineeda } from 'ineeda';
import { stub } from 'sinon';

import { INJECT_CLOCK } from '../../src/BaseService';
import { BotServiceOptions } from '../../src/BotService';
import { User } from '../../src/entity/auth/User';
import { Message } from '../../src/entity/Message';
import { FetchOptions, ListenerData } from '../../src/listener';
import { SessionListener } from '../../src/listener/SessionListener';
import { Clock } from '../../src/utils/Clock';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';

class StubSessionListener extends SessionListener<ListenerData> {
constructor(options: BotServiceOptions<ListenerData>) {
super(options, 'isolex#/definitions/service-listener');
}

public async send(msg: Message) {
/* noop */
}

public async fetch(options: FetchOptions) {
return [];
}
}

const TEST_LOCALE = {
date: 'en-US',
lang: 'en-US',
time: 'en-US',
timezone: 'en-US',
};

const TEST_DATA = {
filters: [],
strict: false,
};

const TEST_METADATA = {
kind: 'test-listener',
name: 'test-listener',
};

describeLeaks('session listener', async () => {
itLeaks('should create a sesion with the current time', async () => {
const stubDate = stub(new Date());
const clock = ineeda<Clock>({
getDate: () => stubDate,
});

const { container } = await createServiceContainer();
const listener = await createService(container, StubSessionListener, {
data: TEST_DATA,
metadata: TEST_METADATA,
[INJECT_CLOCK]: clock,
});

const session = await listener.createSession('test', new User({
locale: TEST_LOCALE,
name: '',
roles: [],
}));
expect(session.createdAt).to.equal(stubDate);
});

itLeaks('should store the created sessions', async () => {
const stubDate = stub(new Date());
const clock = ineeda<Clock>({
getDate: () => stubDate,
});

const { container } = await createServiceContainer();
const listener = await createService(container, StubSessionListener, {
data: TEST_DATA,
metadata: TEST_METADATA,
[INJECT_CLOCK]: clock,
});

const session = await listener.createSession('test', new User({
locale: TEST_LOCALE,
name: '',
roles: [],
}));
const fetch = await listener.getSession('test');
expect(fetch).to.equal(session);
});
});
32 changes: 32 additions & 0 deletions test/utils/TestMatchRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describeLeaks('match utility', async () => {
});
expect(results.matched).to.equal(false);
});

itLeaks('should reject a single string when negated', async () => {
const match = createMatch({
negate: true,
operator: RuleOperator.Every,
values: [{
string: 'bar',
}, {
string: 'foo',
}],
});
const results = match.match({
foo: 'bar',
});
expect(results.matched).to.equal(false);
});
});

describeLeaks('type checks', async () => {
Expand Down Expand Up @@ -115,6 +131,22 @@ describeLeaks('match utility', async () => {
});
expect(results.matched).to.equal(true);
});

itLeaks('should match a single string to any when negated', async () => {
const match = createMatch({
negate: true,
operator: RuleOperator.Any,
values: [{
string: 'bar',
}, {
string: 'foo',
}],
});
const results = match.match({
foo: 'bar',
});
expect(results.matched).to.equal(true);
});
});

describeLeaks('match removal', async () => {
Expand Down
4 changes: 4 additions & 0 deletions test/utils/TestMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ describe('math utils', () => {
}
});

it('should format symbol results as unknown', () => {
expect(formatResult(Symbol(), TEST_SCOPE, TEST_OPTIONS)).to.contain('unknown result type');
});

it('should format date results', () => {
const d = new Date();
expect(formatResult(d, TEST_SCOPE, TEST_OPTIONS)).to.include(d.getFullYear());
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@apextoaster/js-utils@^0.1.6":
version "0.1.6"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@apextoaster/js-utils/-/js-utils-0.1.6.tgz#6ce64014981d076eb11b507bd45e065ba8cbe29b"
integrity sha512-jVw8FzNjqb/bnyIj1FBll+tijwsDTQBrhplpYyGFS4QsU9UjT+N+gdLrElyNJNGcAduEYsQHEJ62owLmJW1A/A==
"@apextoaster/js-utils@^0.1.7":
version "0.1.7"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@apextoaster/js-utils/-/js-utils-0.1.7.tgz#0abab0b23a0d94ff96405b46bf5907844685722e"
integrity sha512-NB1n8y6KjKev8+WZNcYVNCi0FYEA6RVHcNjxTki/gQklZNayKPkGP7WtkVF2IuvEo7L6N0sYkWwxRsl+oQWulw==

"@apextoaster/js-yaml-schema@^0.2.0":
version "0.2.0"
Expand Down

0 comments on commit 06ffb42

Please sign in to comment.