generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
merch-card-collection.test.html.js
181 lines (163 loc) · 6.27 KB
/
merch-card-collection.test.html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { mockLana } from './mocks/lana.js';
import { mockFetch } from './mocks/fetch.js';
import { pushState } from '../src/deeplink.js';
import {
appendMiloStyles,
delay,
toggleLargeDesktop,
} from './utils.js';
import '../src/sidenav/merch-sidenav.js';
import '../src/merch-card-collection.js';
import { withWcs } from './mocks/wcs.js';
import mas from './mas.js';
const searchParams = new URLSearchParams(document.location.search);
const prepareTemplate = (
id,
updateSearch = true,
content = document.getElementById('content'),
) => {
content.innerHTML = '';
const template = document.getElementById(id);
const templateContent = template.content.cloneNode(true);
return [
templateContent.querySelector('merch-card-collection'),
() => {
content.appendChild(templateContent);
if (updateSearch && id !== searchParams.get('template')) {
searchParams.set('template', id);
document.location.search = searchParams.toString();
}
},
];
};
window['prepareTemplate'] = prepareTemplate;
const sidenav = document.getElementById('sidenav');
if (sidenav) {
const templates = [...document.getElementsByTagName('template')].map(
({ id, title }) =>
`<button onclick="(prepareTemplate('${id}')[1])()">${title}</button>`,
);
sidenav.appendChild(
document.createRange().createContextualFragment(templates.join('\n')),
);
const currentTemplate = searchParams.get('template');
if (currentTemplate) {
const [, render] = prepareTemplate(currentTemplate);
render();
}
}
const visibleCards = (index) => {
const cards = Array.from(document.querySelectorAll('merch-card'))
.filter((card) => card.style.display !== 'none')
.sort((a, b) => Number(a.style.order) - Number(b.style.order));
if (isNaN(index)) {
return cards;
} else {
return cards[index];
}
};
let merchCards;
const shouldSkipTests = sessionStorage.getItem('skipTests') ? 'true' : 'false';
runTests(async () => {
await toggleLargeDesktop();
mockLana();
await mockFetch(withWcs);
await mas();
if (shouldSkipTests !== 'true') {
describe('merch-card-collection web component', () => {
let render;
beforeEach(() => {
document.location.hash = '';
[merchCards, render] = prepareTemplate('catalogCards', false);
});
it('renders merch-card-collection element', async () => {
document.location.hash = '';
render();
await delay(100);
expect(visibleCards().length).to.equal(93);
});
it('observes/applies deep link parameters', async () => {
document.location.hash = 'filter=photo';
render();
await delay(100);
expect(visibleCards().length).to.equal(5);
expect(visibleCards(0).size).to.equal('super-wide');
document.location.hash = 'filter=photo&types=web';
await delay(100);
expect(visibleCards().length).to.equal(4);
document.location.hash = 'filter=photo&types=web,desktop';
await delay(100);
expect(visibleCards().length).to.equal(5);
});
it('should make single_app card the second card', async () => {
document.location.hash = 'single_app=illustrator';
render();
await delay(100);
expect(visibleCards(1).name).to.equal('illustrator');
});
it('should display a Show More button', async () => {
merchCards.setAttribute('limit', 16);
merchCards.setAttribute('page', 1);
render();
await delay(100);
expect(visibleCards().length).to.equal(16);
const showMoreButton =
merchCards.shadowRoot.querySelector('#footer sp-button');
expect(showMoreButton.isConnected).to.be.true;
showMoreButton.click();
await delay(100);
showMoreButton.click();
await delay(100);
expect(visibleCards().length).to.equal(48);
pushState({ page: 6 });
await delay(100);
expect(showMoreButton.isConnected).to.be.false;
});
it('should refine result on search with multiple words', async () => {
document.location.hash = '';
render();
await delay(100);
pushState({ search: 'All Apps' });
await delay(100);
expect(visibleCards().length).to.equal(1);
expect(merchCards.resultTextSlotName).to.equal(
'searchResultText',
);
});
it('should refine result on search', async () => {
document.location.hash = '';
render();
await delay(100);
pushState({ search: 'acrobat' });
await delay(100);
expect(visibleCards().length).to.equal(10);
expect(merchCards.resultTextSlotName).to.equal(
'searchResultsText',
);
pushState({ search: 'stager' });
await delay(100);
expect(visibleCards().length).to.equal(1);
expect(merchCards.resultTextSlotName).to.equal(
'searchResultText',
);
pushState({ search: 'cafebabe' });
await delay(100);
expect(visibleCards().length).to.equal(0);
expect(merchCards.resultTextSlotName).to.equal(
'noSearchResultsText',
);
});
});
} else {
appendMiloStyles();
}
});
document.getElementById('showMore').addEventListener('click', () => {
document.location.hash = '';
const [merchCards, render] = prepareTemplate('catalogCards');
merchCards.setAttribute('limit', 16);
merchCards.setAttribute('page', 1);
render();
});