github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/unit/services/breadcrumbs-test.js (about) 1 import Service from '@ember/service'; 2 import Route from '@ember/routing/route'; 3 import Controller from '@ember/controller'; 4 import { get } from '@ember/object'; 5 import { alias } from '@ember/object/computed'; 6 import RSVP from 'rsvp'; 7 import { module, test } from 'qunit'; 8 import { setupTest } from 'ember-qunit'; 9 import PromiseObject from 'nomad-ui/utils/classes/promise-object'; 10 11 const makeRoute = (crumbs, controller = {}) => 12 Route.extend({ 13 breadcrumbs: crumbs, 14 controller: Controller.extend(controller).create(), 15 }); 16 17 module('Unit | Service | Breadcrumbs', function(hooks) { 18 setupTest(hooks); 19 20 hooks.beforeEach(function() { 21 this.subject = function() { 22 return this.owner.factoryFor('service:breadcrumbs').create(); 23 }; 24 }); 25 26 hooks.beforeEach(function() { 27 const mockRouter = Service.extend({ 28 currentRouteName: 'application', 29 currentURL: '/', 30 }); 31 32 this.owner.register('service:router', mockRouter); 33 this.router = this.owner.lookup('service:router'); 34 35 const none = makeRoute(); 36 const fixed = makeRoute([{ label: 'Static', args: ['static.index'] }]); 37 const manyFixed = makeRoute([ 38 { label: 'Static 1', args: ['static.index', 1] }, 39 { label: 'Static 2', args: ['static.index', 2] }, 40 ]); 41 const dynamic = makeRoute(model => [{ label: model, args: ['dynamic.index', model] }], { 42 model: 'Label of the Crumb', 43 }); 44 const manyDynamic = makeRoute( 45 model => [ 46 { label: get(model, 'fishOne'), args: ['dynamic.index', get(model, 'fishOne')] }, 47 { label: get(model, 'fishTwo'), args: ['dynamic.index', get(model, 'fishTwo')] }, 48 ], 49 { 50 model: { 51 fishOne: 'red', 52 fishTwo: 'blue', 53 }, 54 } 55 ); 56 const promise = makeRoute([ 57 PromiseObject.create({ 58 promise: RSVP.Promise.resolve({ 59 label: 'delayed', 60 args: ['wait.for.it'], 61 }), 62 }), 63 ]); 64 const fromURL = makeRoute(model => [{ label: model, args: ['url'] }], { 65 router: this.owner.lookup('service:router'), 66 model: alias('router.currentURL'), 67 }); 68 69 this.owner.register('route:none', none); 70 this.owner.register('route:none.more-none', none); 71 this.owner.register('route:static', fixed); 72 this.owner.register('route:static.many', manyFixed); 73 this.owner.register('route:dynamic', dynamic); 74 this.owner.register('route:dynamic.many', manyDynamic); 75 this.owner.register('route:promise', promise); 76 this.owner.register('route:url', fromURL); 77 }); 78 79 test('when the route hierarchy has no breadcrumbs', function(assert) { 80 this.router.set('currentRouteName', 'none'); 81 82 const service = this.subject(); 83 assert.deepEqual(service.get('breadcrumbs'), []); 84 }); 85 86 test('when the route hierarchy has one segment with static crumbs', function(assert) { 87 this.router.set('currentRouteName', 'static'); 88 89 const service = this.subject(); 90 assert.deepEqual(service.get('breadcrumbs'), [{ label: 'Static', args: ['static.index'] }]); 91 }); 92 93 test('when the route hierarchy has multiple segments with static crumbs', function(assert) { 94 this.router.set('currentRouteName', 'static.many'); 95 96 const service = this.subject(); 97 assert.deepEqual(service.get('breadcrumbs'), [ 98 { label: 'Static', args: ['static.index'] }, 99 { label: 'Static 1', args: ['static.index', 1] }, 100 { label: 'Static 2', args: ['static.index', 2] }, 101 ]); 102 }); 103 104 test('when the route hierarchy has a function as its breadcrumbs property', function(assert) { 105 this.router.set('currentRouteName', 'dynamic'); 106 107 const service = this.subject(); 108 assert.deepEqual(service.get('breadcrumbs'), [ 109 { label: 'Label of the Crumb', args: ['dynamic.index', 'Label of the Crumb'] }, 110 ]); 111 }); 112 113 test('when the route hierarchy has multiple segments with dynamic crumbs', function(assert) { 114 this.router.set('currentRouteName', 'dynamic.many'); 115 116 const service = this.subject(); 117 assert.deepEqual(service.get('breadcrumbs'), [ 118 { label: 'Label of the Crumb', args: ['dynamic.index', 'Label of the Crumb'] }, 119 { label: 'red', args: ['dynamic.index', 'red'] }, 120 { label: 'blue', args: ['dynamic.index', 'blue'] }, 121 ]); 122 }); 123 124 test('when a route provides a breadcrumb that is a promise, it gets passed through to the template', function(assert) { 125 this.router.set('currentRouteName', 'promise'); 126 127 const service = this.subject(); 128 assert.ok(service.get('breadcrumbs.firstObject') instanceof PromiseObject); 129 }); 130 131 // This happens when transitioning to the current route but with a different model 132 // jobs.job.index --> jobs.job.index 133 // /jobs/one --> /jobs/two 134 test('when the route stays the same but the url changes, breadcrumbs get recomputed', function(assert) { 135 this.router.set('currentRouteName', 'url'); 136 137 const service = this.subject(); 138 assert.deepEqual( 139 service.get('breadcrumbs'), 140 [{ label: '/', args: ['url'] }], 141 'The label is initially / as is the router currentURL' 142 ); 143 144 this.router.set('currentURL', '/somewhere/else'); 145 assert.deepEqual( 146 service.get('breadcrumbs'), 147 [{ label: '/somewhere/else', args: ['url'] }], 148 'The label changes with currentURL since it is an alias and a change to currentURL recomputes breadcrumbs' 149 ); 150 }); 151 });