|
1 | | -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
2 | | -import { getPlatform, platformCore } from '@angular/core'; |
3 | | -import { platformBrowser } from '@angular/platform-browser'; |
4 | | -import { |
5 | | - platformBrowserDynamicTesting, |
6 | | - BrowserDynamicTestingModule |
7 | | -} from '@angular/platform-browser-dynamic/testing'; |
8 | | -import { getTestBed } from '@angular/core/testing'; |
| 1 | +import { ɵglobal } from '@angular/core'; |
| 2 | + |
9 | 3 | import { isAngularInTestMode } from '@ngxs/store/internals'; |
10 | 4 |
|
11 | 5 | describe('[utils/angular]', () => { |
12 | 6 | describe('isAngularInTestMode', () => { |
13 | | - function resetEnv() { |
14 | | - const fn = <any>isAngularInTestMode; |
15 | | - // Reset the memoization |
16 | | - fn && fn.reset && fn.reset(); |
17 | | - |
18 | | - // Reset the angular platform |
19 | | - const p = <any>getPlatform(); |
20 | | - p && p.destroy && p.destroy(); |
21 | | - } |
22 | | - |
23 | | - beforeEach(() => { |
24 | | - resetEnv(); |
| 7 | + // Just an empty object so `typeof !== undefined` will be truthy. |
| 8 | + const nonUndefinedValue = {}; |
| 9 | + |
| 10 | + it('should return true if `__karma__` is available', () => { |
| 11 | + // Arrange & act & assert |
| 12 | + const __karma__ = ɵglobal.__karma__; |
| 13 | + ɵglobal.__karma__ = nonUndefinedValue; |
| 14 | + expect(isAngularInTestMode()).toEqual(true); |
| 15 | + ɵglobal.__karma__ = __karma__; |
25 | 16 | }); |
26 | 17 |
|
27 | | - afterEach(() => { |
28 | | - resetEnv(); |
29 | | - |
30 | | - getTestBed().resetTestEnvironment(); |
31 | | - getTestBed().initTestEnvironment( |
32 | | - BrowserDynamicTestingModule, |
33 | | - platformBrowserDynamicTesting() |
34 | | - ); |
35 | | - }); |
36 | | - |
37 | | - it(`should return true if the Angular Test Module has been bootstrapped`, () => { |
38 | | - // Arrange |
39 | | - platformBrowserDynamicTesting(); |
40 | | - // Act |
41 | | - const result = isAngularInTestMode(); |
42 | | - // Assert |
43 | | - expect(result).toEqual(true); |
44 | | - }); |
45 | | - |
46 | | - it(`should return false if Angular has not been bootstrapped`, () => { |
47 | | - // Arrange |
48 | | - |
49 | | - // Act |
50 | | - const result = isAngularInTestMode(); |
51 | | - // Assert |
52 | | - expect(result).toEqual(false); |
| 18 | + it('should return true if `jasmine` is available', () => { |
| 19 | + // Arrange & act & assert |
| 20 | + const jasmine = ɵglobal.jasmine; |
| 21 | + ɵglobal.jasmine = nonUndefinedValue; |
| 22 | + expect(isAngularInTestMode()).toEqual(true); |
| 23 | + ɵglobal.jasmine = jasmine; |
53 | 24 | }); |
54 | 25 |
|
55 | | - it(`should return false if the Angular platformBrowserDynamic has been bootstrapped`, async () => { |
56 | | - // Arrange |
57 | | - platformBrowserDynamic(); |
58 | | - // Act |
59 | | - const result = isAngularInTestMode(); |
60 | | - // Assert |
61 | | - expect(result).toEqual(false); |
| 26 | + it('should return true if `jest` is available', () => { |
| 27 | + // Arrange & act & assert |
| 28 | + const jest = ɵglobal.jest; |
| 29 | + ɵglobal.jest = nonUndefinedValue; |
| 30 | + expect(isAngularInTestMode()).toEqual(true); |
| 31 | + ɵglobal.jest = jest; |
62 | 32 | }); |
63 | 33 |
|
64 | | - it(`should return false if the Angular platformBrowser has been bootstrapped`, async () => { |
65 | | - // Arrange |
66 | | - platformBrowser(); |
67 | | - // Act |
68 | | - const result = isAngularInTestMode(); |
69 | | - // Assert |
70 | | - expect(result).toEqual(false); |
| 34 | + it('should return true if `Mocha` is available', () => { |
| 35 | + // Arrange & act & assert |
| 36 | + const Mocha = ɵglobal.Mocha; |
| 37 | + ɵglobal.Mocha = nonUndefinedValue; |
| 38 | + expect(isAngularInTestMode()).toEqual(true); |
| 39 | + ɵglobal.Mocha = Mocha; |
71 | 40 | }); |
72 | 41 |
|
73 | | - it(`should return false if the Angular platformCore has been bootstrapped`, async () => { |
74 | | - // Arrange |
75 | | - platformCore(); |
76 | | - // Act |
77 | | - const result = isAngularInTestMode(); |
78 | | - // Assert |
79 | | - expect(result).toEqual(false); |
| 42 | + it('should return false if any of the values are globally available', () => { |
| 43 | + // Arrange & act & assert |
| 44 | + const __karma__ = ɵglobal.__karma__; |
| 45 | + const jasmine = ɵglobal.jasmine; |
| 46 | + const jest = ɵglobal.jest; |
| 47 | + const Mocha = ɵglobal.Mocha; |
| 48 | + delete ɵglobal.__karma__; |
| 49 | + delete ɵglobal.jasmine; |
| 50 | + delete ɵglobal.jest; |
| 51 | + delete ɵglobal.Mocha; |
| 52 | + expect(isAngularInTestMode()).toEqual(false); |
| 53 | + ɵglobal.__karma__ = __karma__; |
| 54 | + ɵglobal.jasmine = jasmine; |
| 55 | + ɵglobal.jest = jest; |
| 56 | + ɵglobal.Mocha = Mocha; |
80 | 57 | }); |
81 | 58 | }); |
82 | 59 | }); |
0 commit comments