1
1
import Toggle from '../../../src/components/toggle' ;
2
2
import View from '../../../src/view' ;
3
+ import { assert } from 'chai' ;
4
+ import iframe from '../../../src/iframe' ;
3
5
4
6
describe ( 'Toggle View class' , ( ) => {
5
7
let toggle ;
@@ -25,6 +27,8 @@ describe('Toggle View class', () => {
25
27
let addClassStub ;
26
28
let removeClassStub ;
27
29
let resizeStub ;
30
+ let insertAdjacentHtmlStub ;
31
+ const summaryHtml = '<span>summary</span>' ;
28
32
29
33
beforeEach ( ( ) => {
30
34
superRenderStub = sinon . stub ( View . prototype , 'render' ) ;
@@ -34,13 +38,19 @@ describe('Toggle View class', () => {
34
38
toggle . view = Object . defineProperty ( toggle . view , 'isVisible' , {
35
39
writable : true ,
36
40
} ) ;
41
+ toggle . view = Object . defineProperty ( toggle . view , 'summaryHtml' , {
42
+ writable : true ,
43
+ value : summaryHtml ,
44
+ } ) ;
45
+ insertAdjacentHtmlStub = sinon . stub ( toggle . view . node , 'insertAdjacentHTML' ) ;
37
46
} ) ;
38
47
39
48
afterEach ( ( ) => {
40
49
superRenderStub . restore ( ) ;
41
50
addClassStub . restore ( ) ;
42
51
removeClassStub . restore ( ) ;
43
52
resizeStub . restore ( ) ;
53
+ insertAdjacentHtmlStub . restore ( ) ;
44
54
} ) ;
45
55
46
56
it ( 'calls super\'s render()' , ( ) => {
@@ -75,36 +85,47 @@ describe('Toggle View class', () => {
75
85
} ) ;
76
86
77
87
describe ( 'when iframe exists' , ( ) => {
78
- let setAttributeSpy ;
88
+ let parentSetAttributeSpy ;
89
+ let elSetAttributeSpy ;
79
90
beforeEach ( ( ) => {
80
- setAttributeSpy = sinon . spy ( ) ;
91
+ parentSetAttributeSpy = sinon . spy ( ) ;
92
+ elSetAttributeSpy = sinon . spy ( ) ;
81
93
toggle . view . iframe = {
82
94
parent : {
83
- setAttribute : setAttributeSpy ,
95
+ setAttribute : parentSetAttributeSpy ,
84
96
} ,
97
+ el : {
98
+ setAttribute : elSetAttributeSpy ,
99
+ }
85
100
} ;
86
101
toggle . view . render ( ) ;
87
102
} ) ;
88
103
89
- it ( 'updates three attributes' , ( ) => {
90
- assert . calledThrice ( setAttributeSpy ) ;
104
+ it ( 'updates two attributes on the iframe\'s parent and one attribute on the iframe\'s el' , ( ) => {
105
+ assert . calledTwice ( parentSetAttributeSpy ) ;
106
+ assert . calledOnce ( elSetAttributeSpy ) ;
91
107
} ) ;
92
108
93
109
it ( 'sets tabindex of iframe\'s parent to zero' , ( ) => {
94
- assert . calledWith ( setAttributeSpy . getCall ( 0 ) , 'tabindex' , 0 ) ;
110
+ assert . calledWith ( parentSetAttributeSpy . getCall ( 0 ) , 'tabindex' , 0 ) ;
95
111
} ) ;
96
112
97
113
it ( 'sets role of iframe\'s parent to button' , ( ) => {
98
- assert . calledWith ( setAttributeSpy . getCall ( 1 ) , 'role' , 'button' ) ;
114
+ assert . calledWith ( parentSetAttributeSpy . getCall ( 1 ) , 'role' , 'button' ) ;
99
115
} ) ;
100
116
101
- it ( 'sets aria-label of iframe\'s parent to text title ' , ( ) => {
102
- assert . calledWith ( setAttributeSpy . getCall ( 2 ) , 'aria-label ' , toggle . options . text . title ) ;
117
+ it ( 'sets aria-hidden to true on the iframe\'s el ' , ( ) => {
118
+ assert . calledWith ( elSetAttributeSpy . getCall ( 0 ) , 'aria-hidden ' , true ) ;
103
119
} ) ;
104
120
105
121
it ( 'resizes view' , ( ) => {
106
122
assert . calledOnce ( resizeStub ) ;
107
123
} ) ;
124
+
125
+ it ( 'inserts the summaryHtml at the beginning of the node' , ( ) => {
126
+ assert . calledOnce ( insertAdjacentHtmlStub ) ;
127
+ assert . calledWith ( insertAdjacentHtmlStub , 'afterbegin' , summaryHtml ) ;
128
+ } ) ;
108
129
} ) ;
109
130
} ) ;
110
131
@@ -284,5 +305,55 @@ describe('Toggle View class', () => {
284
305
assert . equal ( toggle . view . readableLabel , `<p class="shopify-buy--visually-hidden">${ toggle . options . text . title } </p>` ) ;
285
306
} ) ;
286
307
} ) ;
308
+
309
+ describe ( 'accessibilityLabel' , ( ) => {
310
+ it ( 'returns the title wrapped in a span' , ( ) => {
311
+ assert . equal ( toggle . view . accessibilityLabel , `<span>${ toggle . options . text . title } </span>` )
312
+ } ) ;
313
+ } ) ;
314
+
315
+ describe ( 'countAccessibilityLabel' , ( ) => {
316
+ it ( 'returns an empty string if count is false in the options contents' , ( ) => {
317
+ toggle . config . toggle = {
318
+ contents : { count : false } ,
319
+ text : { countAccessibilityLabel : 'count label' } ,
320
+ } ;
321
+
322
+ assert . equal ( toggle . view . countAccessibilityLabel , '' ) ;
323
+ } ) ;
324
+
325
+ it ( 'returns the accessibililty label with the count wrapped in a span if count is true in the options contents' , ( ) => {
326
+ const count = 2 ;
327
+ toggle = Object . defineProperty ( toggle , 'count' , {
328
+ writable : true ,
329
+ } ) ;
330
+ toggle . count = count ;
331
+ toggle . config . toggle = {
332
+ contents : { count : true } ,
333
+ text : { countAccessibilityLabel : 'count label' } ,
334
+ } ;
335
+
336
+ assert . equal ( toggle . view . countAccessibilityLabel , `<span>${ toggle . options . text . countAccessibilityLabel } ${ count } </span>` ) ;
337
+ } ) ;
338
+ } ) ;
339
+
340
+ describe ( 'summaryHtml' , ( ) => {
341
+ it ( 'returns the accessibilityLabel and countAccessibilityLabel wrapped in a visually hidden span' , ( ) => {
342
+ const accessibilityLabel = 'accessibility label' ;
343
+ const countAccessibilityLabel = 'count accessibility label' ;
344
+
345
+ toggle . view = Object . defineProperty ( toggle . view , 'accessibilityLabel' , {
346
+ writable : true ,
347
+ value : accessibilityLabel ,
348
+ } ) ;
349
+ toggle . view = Object . defineProperty ( toggle . view , 'countAccessibilityLabel' , {
350
+ writable : true ,
351
+ value : countAccessibilityLabel ,
352
+ } ) ;
353
+
354
+ assert . equal ( toggle . view . summaryHtml , `<span class="shopify-buy--visually-hidden">${ accessibilityLabel } ${ countAccessibilityLabel } </span>` )
355
+ } ) ;
356
+ } ) ;
357
+
287
358
} ) ;
288
359
} ) ;
0 commit comments