11import { expect } from '@esm-bundle/chai' ;
22import sinon from 'sinon' ;
33import { fixtureSync } from '@vaadin/testing-helpers' ;
4- import { getIconId } from '../vaadin-iconset.js' ;
54import { unsafeSvgLiteral } from '../src/vaadin-icon-svg.js' ;
65import '../vaadin-icon.js' ;
76
87const ANGLE_DOWN = '<path d="M13 4v2l-5 5-5-5v-2l5 5z"></path>' ;
98const ANGLE_UP = '<path d="M3 12v-2l5-5 5 5v2l-5-5z"></path>' ;
109
1110describe ( 'vaadin-icon' , ( ) => {
12- let icon ;
11+ let icon , svgElement ;
1312
14- function expectIcon ( content , size = 16 ) {
15- const svgElement = icon . shadowRoot . querySelector ( 'svg' ) ;
13+ function expectIcon ( content ) {
1614 expect ( svgElement . innerHTML . trim ( ) . replace ( / < ! - - [ ^ > ] * - - > / g, '' ) ) . to . equal ( content ) ;
17- expect ( svgElement . getAttribute ( 'viewBox' ) ) . to . equal ( `0 0 ${ size } ${ size } ` ) ;
1815 }
1916
2017 describe ( 'custom element definition' , ( ) => {
@@ -34,6 +31,7 @@ describe('vaadin-icon', () => {
3431 describe ( 'svg property' , ( ) => {
3532 beforeEach ( ( ) => {
3633 icon = fixtureSync ( '<vaadin-icon></vaadin-icon>' ) ;
34+ svgElement = icon . shadowRoot . querySelector ( 'svg' ) ;
3735 } ) ;
3836
3937 describe ( 'valid icon' , ( ) => {
@@ -42,17 +40,11 @@ describe('vaadin-icon', () => {
4240 expectIcon ( ANGLE_DOWN ) ;
4341 } ) ;
4442
45- it ( 'should update icon when size property is set' , ( ) => {
46- icon . size = 24 ;
43+ it ( 'should update icon when svg property is updated' , ( ) => {
4744 icon . svg = unsafeSvgLiteral ( ANGLE_DOWN ) ;
48- expectIcon ( ANGLE_DOWN , 24 ) ;
49- } ) ;
50-
51- it ( 'should update icon when size property is updated' , ( ) => {
52- icon . svg = unsafeSvgLiteral ( ANGLE_DOWN ) ;
53- expectIcon ( ANGLE_DOWN , 16 ) ;
54- icon . size = 24 ;
55- expectIcon ( ANGLE_DOWN , 24 ) ;
45+ expectIcon ( ANGLE_DOWN ) ;
46+ icon . svg = unsafeSvgLiteral ( ANGLE_UP ) ;
47+ expectIcon ( ANGLE_UP ) ;
5648 } ) ;
5749 } ) ;
5850
@@ -87,6 +79,26 @@ describe('vaadin-icon', () => {
8779 } ) ;
8880 } ) ;
8981
82+ describe ( 'size property' , ( ) => {
83+ beforeEach ( ( ) => {
84+ icon = fixtureSync ( '<vaadin-icon></vaadin-icon>' ) ;
85+ svgElement = icon . shadowRoot . querySelector ( 'svg' ) ;
86+ } ) ;
87+
88+ it ( 'should set size property to 24 by default' , ( ) => {
89+ expect ( icon . size ) . to . equal ( 24 ) ;
90+ } ) ;
91+
92+ it ( 'should set viewBox attribute based on size' , ( ) => {
93+ expect ( svgElement . getAttribute ( 'viewBox' ) ) . to . equal ( '0 0 24 24' ) ;
94+ } ) ;
95+
96+ it ( 'should update viewBox attribute on size change' , ( ) => {
97+ icon . size = 16 ;
98+ expect ( svgElement . getAttribute ( 'viewBox' ) ) . to . equal ( '0 0 16 16' ) ;
99+ } ) ;
100+ } ) ;
101+
90102 describe ( 'icon property' , ( ) => {
91103 let iconset , icons ;
92104
@@ -95,8 +107,8 @@ describe('vaadin-icon', () => {
95107 <vaadin-iconset name="vaadin">
96108 <svg xmlns="http://www.w3.org/2000/svg">
97109 <defs>
98- <g id="vaadin-icon :angle-down">${ ANGLE_DOWN } </g>
99- <g id="vaadin-icon :angle-up">${ ANGLE_UP } </g>
110+ <g id="vaadin:angle-down">${ ANGLE_DOWN } </g>
111+ <g id="vaadin:angle-up">${ ANGLE_UP } </g>
100112 </defs>
101113 </svg>
102114 </vaadin-iconset>
@@ -107,24 +119,25 @@ describe('vaadin-icon', () => {
107119 describe ( 'default' , ( ) => {
108120 beforeEach ( ( ) => {
109121 icon = fixtureSync ( '<vaadin-icon></vaadin-icon>' ) ;
122+ svgElement = icon . shadowRoot . querySelector ( 'svg' ) ;
110123 } ) ;
111124
112125 it ( 'should render icon from the iconset' , ( ) => {
113126 icons . forEach ( ( svgIcon ) => {
114- icon . icon = getIconId ( svgIcon . getAttribute ( 'id' ) ) ;
127+ icon . icon = svgIcon . getAttribute ( 'id' ) ;
115128 expectIcon ( svgIcon . innerHTML ) ;
116129 } ) ;
117130 } ) ;
118131
119132 it ( 'should clear icon when the value is set to null' , ( ) => {
120- icon . icon = 'angle-up' ;
133+ icon . icon = 'vaadin: angle-up' ;
121134 expectIcon ( ANGLE_UP ) ;
122135 icon . icon = null ;
123136 expectIcon ( '' ) ;
124137 } ) ;
125138
126139 it ( 'should clear icon when the value is set to undefined' , ( ) => {
127- icon . icon = 'angle-up' ;
140+ icon . icon = 'vaadin: angle-up' ;
128141 expectIcon ( ANGLE_UP ) ;
129142 icon . icon = undefined ;
130143 expectIcon ( '' ) ;
@@ -140,9 +153,10 @@ describe('vaadin-icon', () => {
140153 document . body . removeChild ( icon ) ;
141154 } ) ;
142155
143- it ( 'should clear icon when the value is set to null ' , ( ) => {
144- icon . icon = 'angle-up' ;
156+ it ( 'should set icon when the value is set before attach ' , ( ) => {
157+ icon . icon = 'vaadin: angle-up' ;
145158 document . body . appendChild ( icon ) ;
159+ svgElement = icon . shadowRoot . querySelector ( 'svg' ) ;
146160 expectIcon ( ANGLE_UP ) ;
147161 } ) ;
148162 } ) ;
0 commit comments