refactor state handling to make application available independent of the state
This commit is contained in:
60
tests/unit/application/ApplicationFactory.spec.ts
Normal file
60
tests/unit/application/ApplicationFactory.spec.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { ApplicationFactory, ApplicationGetter } from '@/application/ApplicationFactory';
|
||||
import { ApplicationStub } from '../stubs/ApplicationStub';
|
||||
|
||||
describe('ApplicationFactory', () => {
|
||||
describe('ctor', () => {
|
||||
it('throws if getter is undefined', () => {
|
||||
// arrange
|
||||
const expectedError = 'undefined getter';
|
||||
const getter = undefined;
|
||||
// act
|
||||
const act = () => new SystemUnderTest(getter);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('getAppAsync', () => {
|
||||
it('returns result from the getter', async () => {
|
||||
// arrange
|
||||
const expected = new ApplicationStub();
|
||||
const getter: ApplicationGetter = () => expected;
|
||||
const sut = new SystemUnderTest(getter);
|
||||
// act
|
||||
const actual = await Promise.all( [
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
]);
|
||||
// assert
|
||||
expect(actual.every((value) => value === expected));
|
||||
});
|
||||
it('only executes getter once', async () => {
|
||||
// arrange
|
||||
let totalExecution = 0;
|
||||
const expected = new ApplicationStub();
|
||||
const getter: ApplicationGetter = () => {
|
||||
totalExecution++;
|
||||
return expected;
|
||||
};
|
||||
const sut = new SystemUnderTest(getter);
|
||||
// act
|
||||
await Promise.all( [
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
sut.getAppAsync(),
|
||||
]);
|
||||
// assert
|
||||
expect(totalExecution).to.equal(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class SystemUnderTest extends ApplicationFactory {
|
||||
public constructor(costlyGetter: ApplicationGetter) {
|
||||
super(costlyGetter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
import { CategoryCollectionStub } from '../../stubs/CategoryCollectionStub';
|
||||
import { EnvironmentStub } from '../../stubs/EnvironmentStub';
|
||||
import { ApplicationStub } from '../../stubs/ApplicationStub';
|
||||
import { buildContextAsync } from '@/application/Context/ApplicationContextFactory';
|
||||
import { IApplicationFactory } from '@/application/IApplicationFactory';
|
||||
import { IApplication } from '@/domain/IApplication';
|
||||
|
||||
describe('ApplicationContextFactory', () => {
|
||||
describe('buildContextAsync', () => {
|
||||
describe('factory', () => {
|
||||
it('sets application from factory', async () => {
|
||||
// arrange
|
||||
const expected = new ApplicationStub().withCollection(
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.macOS));
|
||||
const factoryMock = mockFactoryWithApp(expected);
|
||||
// act
|
||||
const context = await buildContextAsync(factoryMock);
|
||||
// assert
|
||||
expect(expected).to.equal(context.app);
|
||||
});
|
||||
});
|
||||
describe('environment', () => {
|
||||
describe('sets initial OS as expected', () => {
|
||||
it('returns currentOs if it is supported', async () => {
|
||||
// arrange
|
||||
const expected = OperatingSystem.Windows;
|
||||
const environment = new EnvironmentStub().withOs(expected);
|
||||
const collection = new CategoryCollectionStub().withOs(expected);
|
||||
const factoryMock = mockFactoryWithCollection(collection);
|
||||
// act
|
||||
const context = await buildContextAsync(factoryMock, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expected).to.equal(actual);
|
||||
});
|
||||
it('fallbacks to other os if OS in environment is not supported', async () => {
|
||||
// arrange
|
||||
const expected = OperatingSystem.Windows;
|
||||
const currentOs = OperatingSystem.macOS;
|
||||
const environment = new EnvironmentStub().withOs(currentOs);
|
||||
const collection = new CategoryCollectionStub().withOs(expected);
|
||||
const factoryMock = mockFactoryWithCollection(collection);
|
||||
// act
|
||||
const context = await buildContextAsync(factoryMock, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expected).to.equal(actual);
|
||||
});
|
||||
it('fallbacks to most supported os if current os is not supported', async () => {
|
||||
// arrange
|
||||
const expectedOs = OperatingSystem.Android;
|
||||
const allCollections = [
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.Linux).withTotalScripts(3),
|
||||
new CategoryCollectionStub().withOs(expectedOs).withTotalScripts(5),
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.Windows).withTotalScripts(4),
|
||||
];
|
||||
const environment = new EnvironmentStub().withOs(OperatingSystem.macOS);
|
||||
const app = new ApplicationStub().withCollections(...allCollections);
|
||||
const factoryMock = mockFactoryWithApp(app);
|
||||
// act
|
||||
const context = await buildContextAsync(factoryMock, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expectedOs).to.equal(actual, `Expected: ${OperatingSystem[expectedOs]}, actual: ${OperatingSystem[actual]}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function mockFactoryWithCollection(result: ICategoryCollection): IApplicationFactory {
|
||||
return mockFactoryWithApp(new ApplicationStub().withCollection(result));
|
||||
}
|
||||
|
||||
function mockFactoryWithApp(app: IApplication): IApplicationFactory {
|
||||
return {
|
||||
getAppAsync: () => Promise.resolve(app),
|
||||
};
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
import { ApplicationParserType, buildContext } from '@/application/Context/ApplicationContextProvider';
|
||||
import { CategoryCollectionStub } from '../../stubs/CategoryCollectionStub';
|
||||
import { EnvironmentStub } from '../../stubs/EnvironmentStub';
|
||||
import { ApplicationStub } from '../../stubs/ApplicationStub';
|
||||
|
||||
describe('ApplicationContextProvider', () => {
|
||||
describe('buildContext', () => {
|
||||
it('sets application from parser', () => {
|
||||
// arrange
|
||||
const expected = new ApplicationStub().withCollection(
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.macOS));
|
||||
const parserMock: ApplicationParserType = () => expected;
|
||||
// act
|
||||
const context = buildContext(parserMock);
|
||||
// assert
|
||||
expect(expected).to.equal(context.app);
|
||||
});
|
||||
describe('sets initial OS as expected', () => {
|
||||
it('returns currentOs if it is supported', () => {
|
||||
// arrange
|
||||
const expected = OperatingSystem.Windows;
|
||||
const environment = new EnvironmentStub().withOs(expected);
|
||||
const parser = mockParser(new CategoryCollectionStub().withOs(expected));
|
||||
// act
|
||||
const context = buildContext(parser, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expected).to.equal(actual);
|
||||
});
|
||||
it('fallbacks to other os if OS in environment is not supported', () => {
|
||||
// arrange
|
||||
const expected = OperatingSystem.Windows;
|
||||
const currentOs = OperatingSystem.macOS;
|
||||
const environment = new EnvironmentStub().withOs(currentOs);
|
||||
const parser = mockParser(new CategoryCollectionStub().withOs(expected));
|
||||
// act
|
||||
const context = buildContext(parser, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expected).to.equal(actual);
|
||||
});
|
||||
it('fallbacks to most supported os if current os is not supported', () => {
|
||||
// arrange
|
||||
const expectedOs = OperatingSystem.Android;
|
||||
const allCollections = [
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.Linux).withTotalScripts(3),
|
||||
new CategoryCollectionStub().withOs(expectedOs).withTotalScripts(5),
|
||||
new CategoryCollectionStub().withOs(OperatingSystem.Windows).withTotalScripts(4),
|
||||
];
|
||||
const environment = new EnvironmentStub().withOs(OperatingSystem.macOS);
|
||||
const app = new ApplicationStub().withCollections(...allCollections);
|
||||
const parser: ApplicationParserType = () => app;
|
||||
// act
|
||||
const context = buildContext(parser, environment);
|
||||
// assert
|
||||
const actual = context.state.os;
|
||||
expect(expectedOs).to.equal(actual, `Expected: ${OperatingSystem[expectedOs]}, actual: ${OperatingSystem[actual]}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function mockParser(result: ICategoryCollection): ApplicationParserType {
|
||||
return () => new ApplicationStub().withCollection(result);
|
||||
}
|
||||
Reference in New Issue
Block a user