github.com/anuvu/nomad@v0.8.7-atom1/ui/tests/integration/job-page/parts/children-test.js (about) 1 import { getOwner } from '@ember/application'; 2 import { assign } from '@ember/polyfills'; 3 import { run } from '@ember/runloop'; 4 import hbs from 'htmlbars-inline-precompile'; 5 import wait from 'ember-test-helpers/wait'; 6 import { findAll, find, click } from 'ember-native-dom-helpers'; 7 import sinon from 'sinon'; 8 import { test, moduleForComponent } from 'ember-qunit'; 9 import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage'; 10 11 moduleForComponent('job-page/parts/children', 'Integration | Component | job-page/parts/children', { 12 integration: true, 13 beforeEach() { 14 window.localStorage.clear(); 15 this.store = getOwner(this).lookup('service:store'); 16 this.server = startMirage(); 17 this.server.create('namespace'); 18 }, 19 afterEach() { 20 this.server.shutdown(); 21 window.localStorage.clear(); 22 }, 23 }); 24 25 const props = (job, options = {}) => 26 assign( 27 { 28 job, 29 sortProperty: 'name', 30 sortDescending: true, 31 currentPage: 1, 32 gotoJob: () => {}, 33 }, 34 options 35 ); 36 37 test('lists each child', function(assert) { 38 let parent; 39 40 this.server.create('job', 'periodic', { 41 id: 'parent', 42 childrenCount: 3, 43 createAllocations: false, 44 }); 45 46 this.store.findAll('job'); 47 48 return wait().then(() => { 49 run(() => { 50 parent = this.store.peekAll('job').findBy('plainId', 'parent'); 51 }); 52 53 this.setProperties(props(parent)); 54 55 this.render(hbs` 56 {{job-page/parts/children 57 job=job 58 sortProperty=sortProperty 59 sortDescending=sortDescending 60 currentPage=currentPage 61 gotoJob=gotoJob}} 62 `); 63 64 return wait().then(() => { 65 assert.equal( 66 findAll('[data-test-job-name]').length, 67 parent.get('children.length'), 68 'A row for each child' 69 ); 70 }); 71 }); 72 }); 73 74 test('eventually paginates', function(assert) { 75 let parent; 76 77 this.server.create('job', 'periodic', { 78 id: 'parent', 79 childrenCount: 11, 80 createAllocations: false, 81 }); 82 83 this.store.findAll('job'); 84 85 return wait().then(() => { 86 run(() => { 87 parent = this.store.peekAll('job').findBy('plainId', 'parent'); 88 }); 89 90 this.setProperties(props(parent)); 91 92 this.render(hbs` 93 {{job-page/parts/children 94 job=job 95 sortProperty=sortProperty 96 sortDescending=sortDescending 97 currentPage=currentPage 98 gotoJob=gotoJob}} 99 `); 100 101 return wait().then(() => { 102 const childrenCount = parent.get('children.length'); 103 assert.ok(childrenCount > 10, 'Parent has more children than one page size'); 104 assert.equal(findAll('[data-test-job-name]').length, 10, 'Table length maxes out at 10'); 105 assert.ok(find('.pagination-next'), 'Next button is rendered'); 106 107 assert.ok( 108 new RegExp(`1.10.+?${childrenCount}`).test(find('.pagination-numbers').textContent.trim()) 109 ); 110 }); 111 }); 112 }); 113 114 test('is sorted based on the sortProperty and sortDescending properties', function(assert) { 115 let parent; 116 117 this.server.create('job', 'periodic', { 118 id: 'parent', 119 childrenCount: 3, 120 createAllocations: false, 121 }); 122 123 this.store.findAll('job'); 124 125 return wait().then(() => { 126 run(() => { 127 parent = this.store.peekAll('job').findBy('plainId', 'parent'); 128 }); 129 130 this.setProperties(props(parent)); 131 132 this.render(hbs` 133 {{job-page/parts/children 134 job=job 135 sortProperty=sortProperty 136 sortDescending=sortDescending 137 currentPage=currentPage 138 gotoJob=gotoJob}} 139 `); 140 141 return wait().then(() => { 142 const sortedChildren = parent.get('children').sortBy('name'); 143 const childRows = findAll('[data-test-job-name]'); 144 145 sortedChildren.reverse().forEach((child, index) => { 146 assert.equal( 147 childRows[index].textContent.trim(), 148 child.get('name'), 149 `Child ${index} is ${child.get('name')}` 150 ); 151 }); 152 153 this.set('sortDescending', false); 154 155 sortedChildren.forEach((child, index) => { 156 assert.equal( 157 childRows[index].textContent.trim(), 158 child.get('name'), 159 `Child ${index} is ${child.get('name')}` 160 ); 161 }); 162 163 return wait(); 164 }); 165 }); 166 }); 167 168 test('gotoJob is called when a job row is clicked', function(assert) { 169 let parent; 170 const gotoJobSpy = sinon.spy(); 171 172 this.server.create('job', 'periodic', { 173 id: 'parent', 174 childrenCount: 1, 175 createAllocations: false, 176 }); 177 178 this.store.findAll('job'); 179 180 return wait().then(() => { 181 run(() => { 182 parent = this.store.peekAll('job').findBy('plainId', 'parent'); 183 }); 184 185 this.setProperties( 186 props(parent, { 187 gotoJob: gotoJobSpy, 188 }) 189 ); 190 191 this.render(hbs` 192 {{job-page/parts/children 193 job=job 194 sortProperty=sortProperty 195 sortDescending=sortDescending 196 currentPage=currentPage 197 gotoJob=gotoJob}} 198 `); 199 200 return wait().then(() => { 201 click('tr.job-row'); 202 assert.ok( 203 gotoJobSpy.withArgs(parent.get('children.firstObject')).calledOnce, 204 'Clicking the job row calls the gotoJob action' 205 ); 206 }); 207 }); 208 });