Files
privacy.sexy/tests/unit/application/Environment/Environment.spec.ts
undergroundwires 5f11c8d98f Migrate unit/integration tests to Vitest with Vite
As part of transition to Vue 3.0 and Vite (#230), this commit
facilitates the shift towards building rest of the application using
Vite. By doing so, it eliminates reliance on outdated Electron building
system that offered limited control, blocking desktop builds (#233).

Changes include:

- Introduce Vite with Vue 2.0 plugin for test execution.
- Remove `mocha`, `chai` and other related dependencies.
- Adjust test to Vitest syntax.
- Revise and update `tests.md` to document the changes.
- Add `@modyfi/vite-plugin-yaml` plugin to be able to use yaml file
  depended logic on test files, replacing previous webpack behavior.
- Fix failing tests that are revealed by Vitest due to unhandled errors
  and lack of assertments.
- Remove the test that depends on Vue CLI populating `process.env`.
- Use `jsdom` for unit test environment, adding it to dependency to
  `package.json` as project now depends on it and it was not specified
  even though `package-lock.json` included it.
2023-08-22 14:02:35 +02:00

122 lines
3.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
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}"`;
}
});
}
});
});
});