Skip to content

Commit eec7bcb

Browse files
christopherthielenmergify[bot]
authored andcommitted
chore(ng13): migrate tests to latest jest, etc
1 parent eea936b commit eec7bcb

File tree

9 files changed

+33
-46
lines changed

9 files changed

+33
-46
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
},
8484
"jest": {
8585
"preset": "jest-preset-angular",
86-
"environments": ["zone.js/testing"],
8786
"setupFilesAfterEnv": [
8887
"./setupJest.ts"
8988
]

test/hooks/onBefore.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export class AppComponent {}
88

99
describe('onBefore hook', () => {
1010
let stateService;
11-
const hook = trans => (stateService = trans.injector().get(StateService));
12-
const hookSpy = jasmine.createSpy('hook', hook).and.callThrough();
11+
const hookSpy = jest.fn(trans => (stateService = trans.injector().get(StateService)));
1312

1413
const setupTests = () => {
1514
const configFn = (router: UIRouter) => {

test/ngModule/deferInitialRender.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('deferInitialRender == false', () => {
6161
status = _status;
6262
}));
6363

64-
it('should not wait for initial transition', async done => {
64+
it('should not wait for initial transition', async () => {
6565
const { stateService } = router;
6666
const fixture = TestBed.createComponent(AppComponent);
6767

@@ -78,7 +78,6 @@ describe('deferInitialRender == false', () => {
7878

7979
resolveData();
8080
await goPromise;
81-
done();
8281
});
8382
});
8483

@@ -95,7 +94,7 @@ describe('deferInitialRender == true', () => {
9594
status = _status;
9695
}));
9796

98-
it('should wait for initial transition', async done => {
97+
it('should wait for initial transition', async () => {
9998
const { stateService } = router;
10099
const fixture = TestBed.createComponent(AppComponent);
101100

@@ -116,7 +115,5 @@ describe('deferInitialRender == true', () => {
116115

117116
await timeout();
118117
expect(status.done).toBe(true);
119-
120-
done();
121118
});
122119
});

test/ngModule/lazyModule.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { async, inject, TestBed } from '@angular/core/testing';
1+
import { inject, TestBed, waitForAsync } from '@angular/core/testing';
22
import { UIRouterModule } from '../../src/uiRouterNgModule';
33
import { UIView } from '../../src/directives/uiView';
44
import { memoryLocationPlugin, UIRouter } from '@uirouter/core';
@@ -42,7 +42,7 @@ describe('lazy loading', () => {
4242
});
4343
});
4444

45-
it('should lazy load a module', async(
45+
it('should lazy load a module', waitForAsync(
4646
inject([UIRouter], (router: UIRouter) => {
4747
const { stateRegistry, stateService, globals } = router;
4848
stateRegistry.register(futureFoo);
@@ -70,7 +70,7 @@ describe('lazy loading', () => {
7070
})
7171
));
7272

73-
it('should throw if no future state replacement is lazy loaded', async(
73+
it('should throw if no future state replacement is lazy loaded', waitForAsync(
7474
inject([UIRouter], (router: UIRouter) => {
7575
const { stateRegistry, stateService } = router;
7676
stateService.defaultErrorHandler(() => null);
@@ -96,7 +96,7 @@ describe('lazy loading', () => {
9696
})
9797
));
9898

99-
it('should support loadChildren on non-future state (manual state cleanup)', async(
99+
it('should support loadChildren on non-future state (manual state cleanup)', waitForAsync(
100100
inject([UIRouter], (router: UIRouter) => {
101101
const { stateRegistry, stateService } = router;
102102
stateRegistry.register(augment1);

test/setupJest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import 'jest-preset-angular';
1+
import 'jest-preset-angular/setup-jest';
22
import './jestGlobalMocks'; // browser mocks globally available for every test

test/testUtils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ import { DebugElement } from '@angular/core';
22
export function clickOnElement(element: DebugElement, button = 0, metaKey = false, ctrlKey = false) {
33
element.triggerEventHandler('click', { button, metaKey, ctrlKey });
44
}
5+
6+
export const tick = () => new Promise((resolve) => setTimeout(resolve));
7+

test/uiSrefActive/uiSrefActive.spec.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Component, Type } from '@angular/core';
2-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44

55
import { UIRouter } from '@uirouter/core';
66
import { UISrefActive } from '../../src';
77
import { UIRouterModule } from '../../src/uiRouterNgModule';
8+
import { tick } from '../testUtils';
89

910
describe('uiSrefActive', () => {
10-
const tick = () => new Promise((resolve) => setTimeout(resolve));
11-
1211
const initialize = <T>(ComponentClass: Type<T>, states) => {
1312
const fixture = TestBed.configureTestingModule({
1413
declarations: [ComponentClass],
@@ -30,7 +29,7 @@ describe('uiSrefActive', () => {
3029
expect(des[0].nativeElement.classList.length).toBe(0);
3130
});
3231

33-
it('applies the class when the link is active', async(() => {
32+
it('applies the class when the link is active', waitForAsync(() => {
3433
const des = fixture.debugElement.queryAll(By.directive(UISrefActive));
3534
const router = fixture.debugElement.injector.get(UIRouter);
3635
router.stateService
@@ -52,7 +51,7 @@ describe('uiSrefActive', () => {
5251
let fixture: ComponentFixture<TestComponent>;
5352
beforeEach(() => (fixture = initialize(TestComponent, [{ name: 'statea' }])));
5453

55-
it('applies all classses when the link is active', async(() => {
54+
it('applies all classses when the link is active', waitForAsync(() => {
5655
const des = fixture.debugElement.queryAll(By.directive(UISrefActive));
5756
const router = fixture.debugElement.injector.get(UIRouter);
5857
router.stateService
@@ -67,7 +66,7 @@ describe('uiSrefActive', () => {
6766
});
6867

6968
describe('on a parent element', () => {
70-
it('applies the active class when any child link is active', async(async () => {
69+
it('applies the active class when any child link is active', waitForAsync(async () => {
7170
const template = `
7271
<li uiSrefActive="active">
7372
<a uiSref="statea">State A</a>
@@ -93,7 +92,7 @@ describe('uiSrefActive', () => {
9392
}));
9493

9594
// Test for https://github.com/ui-router/angular/issues/760
96-
it('can dynamically add or remove nested uiSref', async(async () => {
95+
it('can dynamically add or remove nested uiSref', waitForAsync(async () => {
9796
const template = `
9897
<li id="parent" uiSrefActive="active">
9998
<a uiSref="statea"></a>

test/uiSrefStatus/uiSrefStatus.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Component, DebugElement } from '@angular/core';
2-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
4+
import { UISref } from '../../src';
45

56
import { SrefStatus, UISrefStatus } from '../../src/directives/uiSrefStatus';
67
import { UIRouterModule } from '../../src/uiRouterNgModule';
7-
import { clickOnElement } from '../testUtils';
8+
import { clickOnElement, tick } from '../testUtils';
89

910
describe('uiSrefStatus', () => {
1011
@Component({
@@ -20,7 +21,7 @@ describe('uiSrefStatus', () => {
2021
let de: DebugElement;
2122
let fixture: ComponentFixture<TestComponent>;
2223

23-
beforeEach(async(() => {
24+
beforeEach(waitForAsync(() => {
2425
TestBed.configureTestingModule({
2526
declarations: [TestComponent],
2627
imports: [
@@ -36,20 +37,19 @@ describe('uiSrefStatus', () => {
3637
fixture = TestBed.createComponent(TestComponent);
3738
component = fixture.componentInstance;
3839
fixture.detectChanges();
39-
de = fixture.debugElement.query(By.directive(UISrefStatus));
40+
de = fixture.debugElement.query(By.directive(UISref));
4041
});
4142

4243
describe('when click on `foo` uiSref', () => {
43-
beforeEach(async(() => {
44-
spyOn(component, 'updated');
44+
it('should emit a event with a TargetState pointing to `foo`', async () => {
45+
const spy = jest.spyOn(component, 'updated').mockImplementation(() => {});
4546
clickOnElement(de);
46-
}));
47+
await tick();
48+
expect(spy).toHaveBeenCalledTimes(2);
4749

48-
it('should emit a event with a TargetState pointing to `foo`', () => {
49-
expect(component.updated).toHaveBeenCalled();
50-
const arg: SrefStatus = (component.updated as jasmine.Spy).calls.mostRecent().args[0];
50+
const arg: SrefStatus = spy.mock.calls[1][0];
5151
expect(arg.targetStates.length).toEqual(1);
52-
expect(arg.targetStates[0].state()).toEqual(jasmine.objectContaining({ name: 'foo' }));
52+
expect(arg.targetStates[0].state()).toEqual(expect.objectContaining({ name: 'foo' }));
5353
});
5454
});
5555
});

test/uiView/uiOnParamsChanged.spec.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,27 @@ describe('uiView', () => {
7171

7272
////////////////////////////////////////////////////////////////////////////////
7373

74-
it('should not receive the initial parameter values', async done => {
74+
it('should not receive the initial parameter values', async () => {
7575
expect(lifecycle).toEqual([]);
7676

7777
await router.stateService.go('hasDynamicParams', { param1: 'foo1', param2: 'bar1' });
7878
fixture.detectChanges();
7979

8080
expect(lifecycle).toEqual(['ngOnInit-0']);
8181
expect(paramChanges).toEqual([]);
82-
83-
done();
8482
});
8583

86-
it('should receive dynamic parameter changes', async done => {
84+
it('should receive dynamic parameter changes', async () => {
8785
expect(lifecycle).toEqual([]);
8886

8987
await router.stateService.go('hasDynamicParams', { param1: 'foo3', param2: 'bar3' });
9088
fixture.detectChanges();
9189

9290
expect(lifecycle).toEqual(['ngOnInit-0']);
9391
expect(paramChanges).toEqual([]);
94-
95-
done();
9692
});
9793

98-
it('should receive dynamic parameter changes', async done => {
94+
it('should receive dynamic parameter changes', async () => {
9995
expect(lifecycle).toEqual([]);
10096

10197
await router.stateService.go('hasDynamicParams', { param1: 'foo', param2: 'bar' });
@@ -108,11 +104,9 @@ describe('uiView', () => {
108104

109105
expect(lifecycle).toEqual(['ngOnInit-0']);
110106
expect(paramChanges).toEqual([{ param2: 'notbar' }]);
111-
112-
done();
113107
});
114108

115-
it('should receive multiple parameter changes', async done => {
109+
it('should receive multiple parameter changes', async () => {
116110
expect(lifecycle).toEqual([]);
117111

118112
await router.stateService.go('hasDynamicParams', { param1: 'foo', param2: 'bar' });
@@ -124,11 +118,9 @@ describe('uiView', () => {
124118

125119
expect(lifecycle).toEqual(['ngOnInit-0']);
126120
expect(paramChanges).toEqual([{ param2: 'notbar', param3: 'baz' }]);
127-
128-
done();
129121
});
130122

131-
it('should not receive non-dynamic parameter changes', async done => {
123+
it('should not receive non-dynamic parameter changes', async () => {
132124
expect(lifecycle).toEqual([]);
133125

134126
await router.stateService.go('hasDynamicParams', { param1: 'foo', param2: 'bar' });
@@ -141,8 +133,6 @@ describe('uiView', () => {
141133

142134
expect(lifecycle).toEqual(['ngOnInit-0', 'ngOnDestroy-0', 'ngOnInit-1']);
143135
expect(paramChanges).toEqual([]);
144-
145-
done();
146136
});
147137
});
148138
});

0 commit comments

Comments
 (0)