github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/components/flex-masonry-test.js (about) 1 import { htmlSafe } from '@ember/template'; 2 import { click, find, findAll, render, settled } from '@ember/test-helpers'; 3 import { module, test } from 'qunit'; 4 import { setupRenderingTest } from 'ember-qunit'; 5 import hbs from 'htmlbars-inline-precompile'; 6 import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit'; 7 8 // Used to prevent XSS warnings in console 9 const h = (height) => htmlSafe(`height:${height}px`); 10 11 module('Integration | Component | FlexMasonry', function (hooks) { 12 setupRenderingTest(hooks); 13 14 test('presents as a single div when @items is empty', async function (assert) { 15 assert.expect(4); 16 17 this.setProperties({ 18 items: [], 19 }); 20 21 await render(hbs` 22 <FlexMasonry 23 @items={{this.items}} 24 @columns={{this.columns}}> 25 </FlexMasonry> 26 `); 27 28 const div = find('[data-test-flex-masonry]'); 29 assert.ok(div); 30 assert.equal(div.tagName.toLowerCase(), 'div'); 31 assert.equal(div.children.length, 0); 32 33 await componentA11yAudit(this.element, assert); 34 }); 35 36 test('each item in @items gets wrapped in a flex-masonry-item wrapper', async function (assert) { 37 this.setProperties({ 38 items: ['one', 'two', 'three'], 39 columns: 2, 40 }); 41 42 await render(hbs` 43 <FlexMasonry 44 @items={{this.items}} 45 @columns={{this.columns}} as |item|> 46 <p>{{item}}</p> 47 </FlexMasonry> 48 `); 49 50 assert.equal( 51 findAll('[data-test-flex-masonry-item]').length, 52 this.items.length 53 ); 54 }); 55 56 test('the @withSpacing arg adds the with-spacing class', async function (assert) { 57 await render(hbs` 58 <FlexMasonry 59 @items={{this.items}} 60 @columns={{this.columns}} 61 @withSpacing={{true}}> 62 </FlexMasonry> 63 `); 64 65 assert.ok( 66 find('[data-test-flex-masonry]').classList.contains('with-spacing') 67 ); 68 }); 69 70 test('individual items along with the reflow action are yielded', async function (assert) { 71 this.setProperties({ 72 items: ['one', 'two'], 73 columns: 2, 74 height: h(50), 75 }); 76 77 await render(hbs` 78 <FlexMasonry 79 @items={{this.items}} 80 @columns={{this.columns}} as |item reflow|> 81 <div style={{this.height}} {{on "click" reflow}}>{{item}}</div> 82 </FlexMasonry> 83 `); 84 85 const div = find('[data-test-flex-masonry]'); 86 assert.equal(div.style.maxHeight, '51px'); 87 assert.ok(div.textContent.includes('one')); 88 assert.ok(div.textContent.includes('two')); 89 90 this.set('height', h(500)); 91 await settled(); 92 assert.equal(div.style.maxHeight, '51px'); 93 94 // The height of the div changes when reflow is called 95 await click('[data-test-flex-masonry-item]:first-child div'); 96 97 assert.equal(div.style.maxHeight, '501px'); 98 }); 99 100 test('items are rendered to the DOM in the order they were passed into the component', async function (assert) { 101 assert.expect(4); 102 103 this.setProperties({ 104 items: [ 105 { text: 'One', height: h(20) }, 106 { text: 'Two', height: h(100) }, 107 { text: 'Three', height: h(20) }, 108 { text: 'Four', height: h(20) }, 109 ], 110 columns: 2, 111 }); 112 113 await render(hbs` 114 <FlexMasonry 115 @items={{this.items}} 116 @columns={{this.columns}} as |item|> 117 <div style={{item.height}}>{{item.text}}</div> 118 </FlexMasonry> 119 `); 120 121 findAll('[data-test-flex-masonry-item]').forEach((el, index) => { 122 assert.equal(el.textContent.trim(), this.items[index].text); 123 }); 124 }); 125 126 test('each item gets an order property', async function (assert) { 127 assert.expect(4); 128 129 this.setProperties({ 130 items: [ 131 { text: 'One', height: h(20), expectedOrder: 0 }, 132 { text: 'Two', height: h(100), expectedOrder: 3 }, 133 { text: 'Three', height: h(20), expectedOrder: 1 }, 134 { text: 'Four', height: h(20), expectedOrder: 2 }, 135 ], 136 columns: 2, 137 }); 138 139 await render(hbs` 140 <FlexMasonry 141 @items={{this.items}} 142 @columns={{this.columns}} as |item|> 143 <div style={{item.height}}>{{item.text}}</div> 144 </FlexMasonry> 145 `); 146 147 findAll('[data-test-flex-masonry-item]').forEach((el, index) => { 148 assert.equal(el.style.order, this.items[index].expectedOrder); 149 }); 150 }); 151 152 test('the last item in each column gets a specific flex-basis value', async function (assert) { 153 assert.expect(4); 154 155 this.setProperties({ 156 items: [ 157 { text: 'One', height: h(20) }, 158 { text: 'Two', height: h(100), flexBasis: '100px' }, 159 { text: 'Three', height: h(20) }, 160 { text: 'Four', height: h(100), flexBasis: '100px' }, 161 { text: 'Five', height: h(20), flexBasis: '80px' }, 162 { text: 'Six', height: h(20), flexBasis: '80px' }, 163 ], 164 columns: 4, 165 }); 166 167 await render(hbs` 168 <FlexMasonry 169 @items={{this.items}} 170 @columns={{this.columns}} as |item|> 171 <div style={{item.height}}>{{item.text}}</div> 172 </FlexMasonry> 173 `); 174 175 findAll('[data-test-flex-masonry-item]').forEach((el, index) => { 176 if (el.style.flexBasis) { 177 /* eslint-disable-next-line qunit/no-conditional-assertions */ 178 assert.equal(el.style.flexBasis, this.items[index].flexBasis); 179 } 180 }); 181 }); 182 183 test('when a multi-column layout becomes a single column layout, all inline-styles are reset', async function (assert) { 184 assert.expect(14); 185 186 this.setProperties({ 187 items: [ 188 { text: 'One', height: h(20) }, 189 { text: 'Two', height: h(100) }, 190 { text: 'Three', height: h(20) }, 191 { text: 'Four', height: h(100) }, 192 { text: 'Five', height: h(20) }, 193 { text: 'Six', height: h(20) }, 194 ], 195 columns: 4, 196 }); 197 198 await render(hbs` 199 <FlexMasonry 200 @items={{this.items}} 201 @columns={{this.columns}} as |item|> 202 <div style={{item.height}}>{{item.text}}</div> 203 </FlexMasonry> 204 `); 205 206 assert.equal(find('[data-test-flex-masonry]').style.maxHeight, '101px'); 207 208 this.set('columns', 1); 209 await settled(); 210 211 findAll('[data-test-flex-masonry-item]').forEach((el) => { 212 assert.equal(el.style.flexBasis, ''); 213 assert.equal(el.style.order, ''); 214 }); 215 216 assert.equal(find('[data-test-flex-masonry]').style.maxHeight, ''); 217 }); 218 });