From 4440811e64cd70384a02c4afa97f09eff74f9922 Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Fri, 26 Aug 2016 17:01:44 -0500 Subject: [PATCH] fix(ResolvePolicy): Fix resolve policy config loading Closes #2945 --- src/state/stateBuilder.ts | 10 +++---- test/core/resolveSpec.ts | 55 +++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/state/stateBuilder.ts b/src/state/stateBuilder.ts index 406703944..904dd4d49 100644 --- a/src/state/stateBuilder.ts +++ b/src/state/stateBuilder.ts @@ -132,11 +132,11 @@ function includesBuilder(state: State) { * ] */ export function resolvablesBuilder(state: State): Resolvable[] { - interface Tuple { token: any, val: any, deps: any[], policy?: string } + interface Tuple { token: any, val: any, deps: any[], policy: ResolvePolicy } - /** convert a resolve: {} object to an array of tuples */ - const obj2Tuples = (obj: Obj) => - Object.keys(obj || {}).map(token => ({token, val: obj[token], deps: undefined})); + /** convert resolve: {} and resolvePolicy: {} objects to an array of tuples */ + const objects2Tuples = (resolveObj: Obj, resolvePolicies: { [key: string]: ResolvePolicy }) => + Object.keys(resolveObj || {}).map(token => ({token, val: resolveObj[token], deps: undefined, policy: resolvePolicies[token]})); /** fetch DI annotations from a function or ng1-style array */ const annotate = (fn: Function) => @@ -180,7 +180,7 @@ export function resolvablesBuilder(state: State): Resolvable[] { // If resolveBlock is already an array, use it as-is. // Otherwise, assume it's an object and convert to an Array of tuples let decl = state.resolve; - let items: any[] = isArray(decl) ? decl : obj2Tuples(decl); + let items: any[] = isArray(decl) ? decl : objects2Tuples(decl, state.resolvePolicy || {}); return items.map(item2Resolvable); } diff --git a/test/core/resolveSpec.ts b/test/core/resolveSpec.ts index e57556fdd..a4592a59c 100644 --- a/test/core/resolveSpec.ts +++ b/test/core/resolveSpec.ts @@ -8,12 +8,14 @@ import { omit, pick, forEach, copy } from "../../src/core"; import Spy = jasmine.Spy; import {services} from "../../src/common/coreservices"; import {resolvablesBuilder} from "../../src/state/stateBuilder"; +import {tree2Array} from "../testUtils.ts"; +import {UIRouter} from "../../src/router"; /////////////////////////////////////////////// -var states, statesTree, statesMap: { [key:string]: State } = {}; -var vals, counts, expectCounts; -var asyncCount; +let router, states, statesMap: { [key:string]: State } = {}; +let vals, counts, expectCounts; +let asyncCount; function invokeLater(fn: Function, ctx: ResolveContext) { return new Resolvable("", fn, services.$injector.annotate(fn)).get(ctx) @@ -35,7 +37,14 @@ function getStates() { }, I: { resolve: { _I: function(_I) { return "I"; } } } }, - J: { resolve: { _J: function() { counts['_J']++; return "J"; }, _J2: function(_J) { counts['_J2']++; return _J + "J2"; } }, + J: { + resolve: { + _J: function() { counts['_J']++; return "J"; }, + _J2: function(_J) { counts['_J2']++; return _J + "J2"; } + }, + resolvePolicy: { + _J: { when: 'EAGER' } + }, K: { resolve: { _K: function(_J2) { counts['_K']++; return _J2 + "K"; }}, L: { resolve: { _L: function(_K) { counts['_L']++; return _K + "L"; }}, M: { resolve: { _M: function(_L) { counts['_M']++; return _L + "M"; }} } @@ -57,35 +66,17 @@ function getStates() { }; } - beforeEach(function () { + router = new UIRouter(); + router.stateRegistry.stateQueue.autoFlush(router.stateService); + counts = { _J: 0, _J2: 0, _K: 0, _L: 0, _M: 0, _Q: 0 }; vals = { _Q: null }; expectCounts = copy(counts); - states = getStates(); - var stateProps = ["resolve", "resolvePolicy"]; - statesTree = loadStates({}, states, ''); - - function loadStates(parent, state, name) { - var thisState = pick.apply(null, [state].concat(stateProps)); - var substates = omit.apply(null, [state].concat(stateProps)); - var resolve = thisState.resolve || {}; - - thisState.resolvables = resolvablesBuilder( { resolve }); - thisState.template = thisState.template || "empty"; - thisState.name = name; - thisState.parent = parent.name; - thisState.params = {}; - thisState.data = { children: [] }; - - forEach(substates, function (value, key) { - thisState.data.children.push(loadStates(thisState, value, key)); - }); - thisState = new State(thisState); - statesMap[name] = thisState; - return thisState; - } + tree2Array(getStates(), false).forEach(state => router.stateRegistry.register(state)); + statesMap = router.stateRegistry.get() + .reduce((acc, state) => ((acc[state.name] = state.$$state()), acc), {}); }); function makePath(names: string[]): PathNode[] { @@ -126,6 +117,14 @@ describe('Resolvables system:', function () { }).then(done); }); + it('should resolve only eager resolves when run with "eager" policy', done => { + let path = makePath([ "J", "K" ]); + let ctx = new ResolveContext(path); + ctx.resolvePath("EAGER").then(function () { + expect(getResolvedData(ctx)).toEqualData({ _J: "J" }); + }).then(done); + }); + it('should resolve lazy and eager resolves when run with "lazy" policy', done => { let path = makePath([ "J", "N" ]); let ctx = new ResolveContext(path);