Skip to content

Commit 605e28e

Browse files
test(Transition): refactor test to avoid undefined promise chain order
1 parent a794018 commit 605e28e

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

test/transitionSpec.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ describe('transition', function () {
1818
return $transitions.create(fromPath, $state.target(to, null, options));
1919
}
2020

21-
const wait = (val?) =>
21+
const _delay = (millis) =>
22+
new Promise(resolve => setTimeout(resolve, millis));
23+
const delay = (millis) => () => _delay(millis);
24+
25+
const tick = (val?) =>
2226
new Promise((resolve) => setTimeout(() => resolve(val)));
2327

2428
// Use this in a .then(go('from', 'to')) when you want to run a transition you expect to succeed
2529
const go = (from, to, options?) =>
26-
() => makeTransition(from, to, options).run().then(wait);
30+
() => makeTransition(from, to, options).run().then(tick);
2731

2832
// Use this in a .then(goFail('from', 'to')) when you want to run a transition you expect to fail
2933
const goFail = (from, to, options?) =>
30-
() => makeTransition(from, to, options).run().catch(wait);
34+
() => makeTransition(from, to, options).run().catch(tick);
3135

3236
beforeEach(() => {
3337
router = new UIRouter();
@@ -121,7 +125,7 @@ describe('transition', function () {
121125

122126
var tsecond = makeTransition("", "second");
123127
tsecond.run()
124-
.then(wait)
128+
.then(tick)
125129
.then(() => expect(t).toBe(tsecond))
126130
.then(done);
127131
}));
@@ -132,7 +136,7 @@ describe('transition', function () {
132136
router.stateRegistry.register({
133137
name: 'slowResolve',
134138
resolve: {
135-
foo: () => new Promise(resolve => setTimeout(resolve, 50))
139+
foo: delay(50)
136140
}
137141
});
138142

@@ -143,10 +147,13 @@ describe('transition', function () {
143147

144148
$state.go('slowResolve');
145149

146-
setTimeout(() => $state.go('slowResolve').transition.promise.then(() => {
147-
expect(results).toEqual({success: 1, error: 1});
148-
done();
149-
}), 20);
150+
_delay(20)
151+
.then(() =>
152+
$state.go('slowResolve').transition.promise )
153+
.then(delay(50))
154+
.then(() =>
155+
expect(results).toEqual({ success: 1, error: 1 }))
156+
.then(done)
150157
}));
151158

152159
describe('.onCreate()', function() {
@@ -275,7 +282,7 @@ describe('transition', function () {
275282
var transition = makeTransition("", "third");
276283
var result = new PromiseResult(transition.promise);
277284
transition.run()
278-
.then(wait)
285+
.then(tick)
279286
.then(() => {
280287
expect(result.called()).toEqual({ resolve: true, reject: false, complete: true });
281288
expect(typeof args.trans.from).toBe('function');
@@ -532,7 +539,7 @@ describe('transition', function () {
532539

533540
transition.promise.catch(function(err) { rejection = err; });
534541
transition.run()
535-
.catch(wait)
542+
.catch(tick)
536543
.then(() => {
537544
expect(pluck(states, 'name')).toEqual([ 'A', 'B', 'C' ]);
538545
expect(rejection.type).toEqual(RejectType.ABORTED);
@@ -547,7 +554,7 @@ describe('transition', function () {
547554
transition.promise.catch(function(err) { rejection = err; });
548555

549556
transition.run()
550-
.catch(wait)
557+
.catch(tick)
551558
.then(() => {
552559
expect(pluck(states, 'name')).toEqual([ 'B', 'C' ]);
553560
expect(rejection.type).toEqual(RejectType.SUPERSEDED);
@@ -574,12 +581,12 @@ describe('transition', function () {
574581

575582
transition.promise.catch(function(err) { rejection = err; });
576583
transition.run()
577-
.then(wait, wait)
584+
.then(tick, tick)
578585
.then(() => {
579586
// .onEnter() from A->C should have set transition2.
580587
transition2.promise.then(function() { transition2success = true; });
581588
})
582-
.then(wait, wait)
589+
.then(tick, tick)
583590
.then(() => {
584591
expect(pluck(states, 'name')).toEqual([ 'B', 'C', 'G' ]);
585592
expect(rejection instanceof Rejection).toBeTruthy();
@@ -607,7 +614,7 @@ describe('transition', function () {
607614
});
608615

609616
transition.run()
610-
.then(wait, wait)
617+
.then(tick, tick)
611618
.then(() => expect(log.join('')).toBe("#B^B#C^C#D^D"))
612619
.then(done);
613620
});
@@ -619,7 +626,7 @@ describe('transition', function () {
619626
function resolveDeferredFor(name) {
620627
log.push("^" + name);
621628
defers[name].resolve("ok, go ahead!");
622-
return wait();
629+
return tick();
623630
}
624631

625632
$transitions.onEnter({ entering: '**' }, function waitWhileEnteringState(trans, state) {
@@ -630,7 +637,7 @@ describe('transition', function () {
630637
transition.promise.then(function() { log.push("DONE"); });
631638
transition.run();
632639

633-
wait().then(() => expect(log.join(';')).toBe("#B"))
640+
tick().then(() => expect(log.join(';')).toBe("#B"))
634641

635642
.then(() => resolveDeferredFor("B"))
636643
.then(() => expect(log.join(';')).toBe("#B;^B;#C"))
@@ -667,9 +674,9 @@ describe('transition', function () {
667674

668675
transition.run();
669676

670-
wait().then(() => expect(log.join(';')).toBe("adding resolve;Entered#B;resolving"))
677+
tick().then(() => expect(log.join(';')).toBe("adding resolve;Entered#B;resolving"))
671678
.then(() => defer.resolve("resolvedval"))
672-
.then(wait, wait)
679+
.then(tick, tick)
673680
.then(() => expect(log.join(';')).toBe("adding resolve;Entered#B;resolving;resolvedval;Entered#C;Entered#D;DONE!"))
674681
.then(done, done);
675682
}));

0 commit comments

Comments
 (0)