Files
privacy.sexy/tests/unit/application/Environment/Environment.spec.ts
undergroundwires 5b1fbe1e2f Refactor code to comply with ESLint rules
Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
2022-01-02 18:20:14 +01:00

123 lines
3.5 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { IBrowserOsDetector } from '@/application/Environment/BrowserOs/IBrowserOsDetector';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { Environment, IEnvironmentVariables } from '@/application/Environment/Environment';
import { DesktopOsTestCases } from './DesktopOsTestCases';
interface EnvironmentVariables {
window?: unknown;
process?: unknown;
navigator?: unknown;
}
class SystemUnderTest extends Environment {
constructor(variables: EnvironmentVariables, browserOsDetector?: IBrowserOsDetector) {
super(variables as unknown as IEnvironmentVariables, browserOsDetector);
}
}
describe('Environment', () => {
describe('isDesktop', () => {
it('returns true if process type is renderer', () => {
// arrange
const window = {
process: {
type: 'renderer',
},
};
// act
const sut = new SystemUnderTest({ window });
// assert
expect(sut.isDesktop).to.equal(true);
});
it('returns true if electron is defined as process version', () => {
// arrange
const process = {
versions: {
electron: true,
},
};
// act
const sut = new SystemUnderTest({ process });
// assert
expect(sut.isDesktop).to.equal(true);
});
it('returns true if navigator user agent has electron', () => {
// arrange
const navigator = {
userAgent: 'Electron',
};
// act
const sut = new SystemUnderTest({ navigator });
// assert
expect(sut.isDesktop).to.equal(true);
});
it('returns false as default', () => {
const sut = new SystemUnderTest({});
expect(sut.isDesktop).to.equal(false);
});
});
describe('os', () => {
it('returns undefined without user agent', () => {
// arrange
const expected = undefined;
const mock: IBrowserOsDetector = {
detect: () => {
throw new Error('should not reach here');
},
};
const sut = new SystemUnderTest({}, mock);
// act
const actual = sut.os;
// assert
expect(actual).to.equal(expected);
});
it('browser os from BrowserOsDetector', () => {
// arrange
const givenUserAgent = 'testUserAgent';
const expected = OperatingSystem.macOS;
const window = {
navigator: {
userAgent: givenUserAgent,
},
};
const mock: IBrowserOsDetector = {
detect: (agent) => {
if (agent !== givenUserAgent) {
throw new Error('Unexpected user agent');
}
return expected;
},
};
// act
const sut = new SystemUnderTest({ window }, mock);
const actual = sut.os;
// assert
expect(actual).to.equal(expected);
});
describe('desktop os', () => {
const navigator = {
userAgent: 'Electron',
};
for (const testCase of DesktopOsTestCases) {
it(testCase.processPlatform, () => {
// arrange
const process = {
platform: testCase.processPlatform,
};
// act
const sut = new SystemUnderTest({ navigator, process });
// assert
expect(sut.os).to.equal(testCase.expectedOs, printMessage());
function printMessage(): string {
return `Expected: "${OperatingSystem[testCase.expectedOs]}"\n`
+ `Actual: "${OperatingSystem[sut.os]}"\n`
+ `Platform: "${testCase.processPlatform}"`;
}
});
}
});
});
});