diff --git a/src/application/Environment/Environment.ts b/src/application/Environment/Environment.ts index 647afd06..5f5c6279 100644 --- a/src/application/Environment/Environment.ts +++ b/src/application/Environment/Environment.ts @@ -24,9 +24,12 @@ export class Environment implements IEnvironment { throw new Error('variables is null or empty'); } this.isDesktop = isDesktop(variables); - this.os = this.isDesktop ? - getDesktopOsType(getProcessPlatform(variables)) - : browserOsDetector.detect(getUserAgent(variables)); + if (this.isDesktop) { + this.os = getDesktopOsType(getProcessPlatform(variables)); + } else { + const userAgent = getUserAgent(variables); + this.os = !userAgent ? undefined : browserOsDetector.detect(userAgent); + } } } diff --git a/tests/unit/application/Environment/Environment.spec.ts b/tests/unit/application/Environment/Environment.spec.ts index 5d763383..a71e8e20 100644 --- a/tests/unit/application/Environment/Environment.spec.ts +++ b/tests/unit/application/Environment/Environment.spec.ts @@ -59,7 +59,21 @@ describe('Environment', () => { }); }); 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 const givenUserAgent = 'testUserAgent'; const expected = OperatingSystem.macOS; @@ -87,17 +101,19 @@ describe('Environment', () => { userAgent: 'Electron', }; for (const testCase of DesktopOsTestCases) { - // arrange - const process = { - platform: testCase.processPlatform, - }; - // act - const sut = new SystemUnderTest({ navigator, process }); - // assert - expect(sut.os).to.equal(testCase.expectedOs, - `Expected: "${OperatingSystem[testCase.expectedOs]}"\n` + - `Actual: "${OperatingSystem[sut.os]}"\n` + - `Platform: "${testCase.processPlatform}"`); + it(testCase.processPlatform, () => { + // arrange + const process = { + platform: testCase.processPlatform, + }; + // act + const sut = new SystemUnderTest({ navigator, process }); + // assert + expect(sut.os).to.equal(testCase.expectedOs, + `Expected: "${OperatingSystem[testCase.expectedOs]}"\n` + + `Actual: "${OperatingSystem[sut.os]}"\n` + + `Platform: "${testCase.processPlatform}"`); + }); } }); });