Files
privacy.sexy/tests/unit/application/ApplicationFactory.spec.ts
undergroundwires 44d79e2c9a Add more and unify tests for absent object cases
- Unify test data for nonexistence of an object/string and collection.
- Introduce more test through adding missing test data to existing tests.
- Improve logic for checking absence of values to match tests.
- Add missing tests for absent value validation.
- Update documentation to include shared test functionality.
2022-01-21 22:34:11 +01:00

64 lines
1.8 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { ApplicationFactory, ApplicationGetterType } from '@/application/ApplicationFactory';
import { ApplicationStub } from '@tests/unit/stubs/ApplicationStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('ApplicationFactory', () => {
describe('ctor', () => {
describe('throws if getter is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing getter';
const getter: ApplicationGetterType = absentValue;
// act
const act = () => new SystemUnderTest(getter);
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('getApp', () => {
it('returns result from the getter', async () => {
// arrange
const expected = new ApplicationStub();
const getter: ApplicationGetterType = () => expected;
const sut = new SystemUnderTest(getter);
// act
const actual = await Promise.all([
sut.getApp(),
sut.getApp(),
sut.getApp(),
sut.getApp(),
]);
// assert
expect(actual.every((value) => value === expected));
});
it('only executes getter once', async () => {
// arrange
let totalExecution = 0;
const expected = new ApplicationStub();
const getter: ApplicationGetterType = () => {
totalExecution++;
return expected;
};
const sut = new SystemUnderTest(getter);
// act
await Promise.all([
sut.getApp(),
sut.getApp(),
sut.getApp(),
sut.getApp(),
]);
// assert
expect(totalExecution).to.equal(1);
});
});
});
class SystemUnderTest extends ApplicationFactory {
public constructor(costlyGetter: ApplicationGetterType) {
super(costlyGetter);
}
}