Fix OS desktop detection tests and edge cases
- Fix test cases not running for desktop OS detection. - Fixes application throwing error when user agent is undefined. - Refactor by making os property optional in Environment to explicit describe its potential undefined state.
This commit is contained in:
@@ -24,9 +24,12 @@ export class Environment implements IEnvironment {
|
|||||||
throw new Error('variables is null or empty');
|
throw new Error('variables is null or empty');
|
||||||
}
|
}
|
||||||
this.isDesktop = isDesktop(variables);
|
this.isDesktop = isDesktop(variables);
|
||||||
this.os = this.isDesktop ?
|
if (this.isDesktop) {
|
||||||
getDesktopOsType(getProcessPlatform(variables))
|
this.os = getDesktopOsType(getProcessPlatform(variables));
|
||||||
: browserOsDetector.detect(getUserAgent(variables));
|
} else {
|
||||||
|
const userAgent = getUserAgent(variables);
|
||||||
|
this.os = !userAgent ? undefined : browserOsDetector.detect(userAgent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,21 @@ describe('Environment', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('os', () => {
|
describe('os', () => {
|
||||||
describe('browser os from BrowserOsDetector', () => {
|
it('returns undefined without user agent', () => {
|
||||||
|
// arrange
|
||||||
|
const expected = undefined;
|
||||||
|
const mock: IBrowserOsDetector = {
|
||||||
|
detect: (agent) => {
|
||||||
|
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
|
// arrange
|
||||||
const givenUserAgent = 'testUserAgent';
|
const givenUserAgent = 'testUserAgent';
|
||||||
const expected = OperatingSystem.macOS;
|
const expected = OperatingSystem.macOS;
|
||||||
@@ -87,6 +101,7 @@ describe('Environment', () => {
|
|||||||
userAgent: 'Electron',
|
userAgent: 'Electron',
|
||||||
};
|
};
|
||||||
for (const testCase of DesktopOsTestCases) {
|
for (const testCase of DesktopOsTestCases) {
|
||||||
|
it(testCase.processPlatform, () => {
|
||||||
// arrange
|
// arrange
|
||||||
const process = {
|
const process = {
|
||||||
platform: testCase.processPlatform,
|
platform: testCase.processPlatform,
|
||||||
@@ -98,6 +113,7 @@ describe('Environment', () => {
|
|||||||
`Expected: "${OperatingSystem[testCase.expectedOs]}"\n` +
|
`Expected: "${OperatingSystem[testCase.expectedOs]}"\n` +
|
||||||
`Actual: "${OperatingSystem[sut.os]}"\n` +
|
`Actual: "${OperatingSystem[sut.os]}"\n` +
|
||||||
`Platform: "${testCase.processPlatform}"`);
|
`Platform: "${testCase.processPlatform}"`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user