@@ -20,6 +20,10 @@ import {imgLoadPromise} from './testing/utils';
20
20
const { expect} = chai ;
21
21
const fourByThreeUri = 'data:image/gif;base64,R0lGODdhBAADAIAAAP///////ywAAAAABAADAAACA4SPVgA7' ;
22
22
const threeByFourUri = 'data:image/gif;base64,R0lGODdhAwAEAIAAAP///////ywAAAAAAwAEAAACA4SPVgA7' ;
23
+ const testLeft = 0 ;
24
+ const testTop = 0 ;
25
+ const testWidth = 10 ;
26
+ const testHeight = 10 ;
23
27
24
28
async function updateImg ( img , fit , src ) {
25
29
img . style . objectFit = fit ;
@@ -29,12 +33,12 @@ async function updateImg(img, fit, src) {
29
33
30
34
describe ( 'createImitationImg' , ( ) => {
31
35
let testContainer ;
32
- let img ;
36
+ let srcImg ;
33
37
34
38
beforeEach ( ( ) => {
35
- img = document . createElement ( 'img' ) ;
39
+ srcImg = document . createElement ( 'img' ) ;
36
40
testContainer = document . createElement ( 'div' ) ;
37
- testContainer . appendChild ( img ) ;
41
+ testContainer . appendChild ( srcImg ) ;
38
42
document . body . appendChild ( testContainer ) ;
39
43
} ) ;
40
44
@@ -44,115 +48,126 @@ describe('createImitationImg', () => {
44
48
45
49
describe ( 'for object-fit: contain' , ( ) => {
46
50
beforeEach ( async ( ) => {
47
- await updateImg ( img , 'contain' , threeByFourUri ) ;
48
- img . style . width = '10px' ;
49
- img . style . height = '10px' ;
51
+ await updateImg ( srcImg , 'contain' , threeByFourUri ) ;
52
+ srcImg . style . width = ` ${ testWidth } px` ;
53
+ srcImg . style . height = ` ${ testHeight } px` ;
50
54
} ) ;
51
55
52
56
describe ( 'the scaleElement' , ( ) => {
53
57
it ( 'should size correctly' , async ( ) => {
54
- const { scaleElement} = createImitationImg ( img ) ;
58
+ const { scaleElement} = createImitationImg ( srcImg ) ;
55
59
testContainer . appendChild ( scaleElement ) ;
56
60
57
61
const { width, height} = scaleElement . getBoundingClientRect ( ) ;
58
- expect ( width ) . to . equal ( 10 ) ;
59
- expect ( height ) . to . equal ( 10 ) ;
62
+ expect ( width ) . to . equal ( testWidth ) ;
63
+ expect ( height ) . to . equal ( testHeight ) ;
60
64
} ) ;
61
65
} ) ;
62
66
63
67
describe ( 'the img' , ( ) => {
64
- let scaleElement ;
65
- let replacementImg ;
68
+ let imgElement ;
66
69
67
70
beforeEach ( ( ) => {
68
- const replacement = createImitationImg ( img ) ;
69
- scaleElement = replacement . scaleElement ;
70
- replacementImg = replacement . img ;
71
+ const { img, scaleElement} = createImitationImg ( srcImg ) ;
72
+ imgElement = img ;
71
73
testContainer . appendChild ( scaleElement ) ;
72
74
scaleElement . style . position = 'fixed' ;
73
- scaleElement . style . top = '10px' ;
74
- scaleElement . style . left = '10px' ;
75
+ scaleElement . style . top = ` ${ testTop } px` ;
76
+ scaleElement . style . left = ` ${ testLeft } px` ;
75
77
} )
76
78
77
79
it ( 'should size correctly' , async ( ) => {
78
- const { width, height} = replacementImg . getBoundingClientRect ( ) ;
80
+ const { width, height} = imgElement . getBoundingClientRect ( ) ;
81
+ // (3/4) * testHeight
79
82
expect ( width ) . to . equal ( 7.5 ) ;
83
+ // Larger dimension is height, so should match testHeight
80
84
expect ( height ) . to . equal ( 10 ) ;
81
85
} ) ;
82
86
83
87
it ( 'should position correctly' , async ( ) => {
84
- const { left, top} = replacementImg . getBoundingClientRect ( ) ;
85
- expect ( left ) . to . equal ( 11.25 ) ;
86
- expect ( top ) . to . equal ( 10 ) ;
88
+ const { left, top} = imgElement . getBoundingClientRect ( ) ;
89
+ // Should center the horizontal and has a width of 7.5
90
+ // testLeft + (testWidth - ((3/4) * testWidth)) / 2 = 1.25
91
+ expect ( left ) . to . equal ( 1.25 ) ;
92
+ // Height matches testHeight, so centered at testTop
93
+ expect ( top ) . to . equal ( 0 ) ;
87
94
} ) ;
88
95
} ) ;
89
96
} ) ;
90
97
91
98
describe ( 'for object-fit: cover' , ( ) => {
92
99
describe ( 'for portrait images' , ( ) => {
93
100
beforeEach ( async ( ) => {
94
- await updateImg ( img , 'cover' , threeByFourUri ) ;
95
- img . style . width = '10px' ;
96
- img . style . height = '10px' ;
101
+ await updateImg ( srcImg , 'cover' , threeByFourUri ) ;
102
+ srcImg . style . width = ` ${ testWidth } px` ;
103
+ srcImg . style . height = ` ${ testHeight } px` ;
97
104
} ) ;
98
105
99
106
describe ( 'the img' , ( ) => {
100
- let replacementImg ;
107
+ let imgElement ;
101
108
102
109
beforeEach ( ( ) => {
103
- const replacement = createImitationImg ( img ) ;
104
- const scaleElement = replacement . scaleElement ;
105
- replacementImg = replacement . img ;
110
+ const { img, scaleElement} = createImitationImg ( srcImg ) ;
111
+ imgElement = img ;
106
112
testContainer . appendChild ( scaleElement ) ;
107
113
scaleElement . style . position = 'fixed' ;
108
- scaleElement . style . top = '100px' ;
109
- scaleElement . style . left = '100px' ;
114
+ scaleElement . style . top = ` ${ testTop } px` ;
115
+ scaleElement . style . left = ` ${ testLeft } px` ;
110
116
} )
111
117
112
118
it ( 'should size correctly' , async ( ) => {
113
- const { width, height} = replacementImg . getBoundingClientRect ( ) ;
119
+ const { width, height} = imgElement . getBoundingClientRect ( ) ;
120
+ // Smaller dimension is width, so should match testWidth
114
121
expect ( width ) . to . equal ( 10 ) ;
122
+ // (4/3) * testWidth
115
123
expect ( height ) . to . be . closeTo ( 13.333 , 0.1 ) ;
116
124
} ) ;
117
125
118
126
it ( 'should position correctly' , async ( ) => {
119
- const { left, top} = replacementImg . getBoundingClientRect ( ) ;
120
- expect ( left ) . to . equal ( 100 ) ;
121
- expect ( top ) . to . be . closeTo ( 98.333 , 0.1 ) ;
127
+ const { left, top} = imgElement . getBoundingClientRect ( ) ;
128
+ // Width matches testWidth, so centered at testLeft
129
+ expect ( left ) . to . equal ( 0 ) ;
130
+ // Should center the vertical and has a height of 13.333
131
+ // testLeft + (testWidth - ((4/3) * testWidth)) / 2 = -1.666
132
+ expect ( top ) . to . be . closeTo ( - 1.666 , 0.1 ) ;
122
133
} ) ;
123
134
} ) ;
124
135
} ) ;
125
136
126
137
describe ( 'for landscape images' , ( ) => {
127
138
beforeEach ( async ( ) => {
128
- await updateImg ( img , 'cover' , fourByThreeUri ) ;
129
- img . style . width = '10px' ;
130
- img . style . height = '10px' ;
139
+ await updateImg ( srcImg , 'cover' , fourByThreeUri ) ;
140
+ srcImg . style . width = ` ${ testWidth } px` ;
141
+ srcImg . style . height = ` ${ testHeight } px` ;
131
142
} ) ;
132
143
133
144
describe ( 'the img' , ( ) => {
134
- let replacementImg ;
145
+ let imgElement ;
135
146
136
147
beforeEach ( ( ) => {
137
- const replacement = createImitationImg ( img ) ;
138
- const scaleElement = replacement . scaleElement ;
139
- replacementImg = replacement . img ;
148
+ const { img, scaleElement} = createImitationImg ( srcImg ) ;
149
+ imgElement = img ;
140
150
testContainer . appendChild ( scaleElement ) ;
141
151
scaleElement . style . position = 'fixed' ;
142
- scaleElement . style . top = '100px' ;
143
- scaleElement . style . left = '100px' ;
152
+ scaleElement . style . top = ` ${ testTop } px` ;
153
+ scaleElement . style . left = ` ${ testLeft } px` ;
144
154
} )
145
155
146
156
it ( 'should size correctly' , async ( ) => {
147
- const { width, height} = replacementImg . getBoundingClientRect ( ) ;
157
+ const { width, height} = imgElement . getBoundingClientRect ( ) ;
158
+ // (4/3) * testHeight = 13.333
148
159
expect ( width ) . to . be . closeTo ( 13.333 , 0.1 ) ;
160
+ // Smaller dimension is height, so should match testHeight
149
161
expect ( height ) . to . equal ( 10 ) ;
150
162
} ) ;
151
163
152
164
it ( 'should position correctly' , async ( ) => {
153
- const { left, top} = replacementImg . getBoundingClientRect ( ) ;
154
- expect ( left ) . to . be . closeTo ( 98.333 , 0.1 ) ;
155
- expect ( top ) . to . equal ( 100 ) ;
165
+ const { left, top} = imgElement . getBoundingClientRect ( ) ;
166
+ // Should center the horizontal and has a width of 13.333
167
+ // testLeft + (testHeight - ((4/3) * testHeight)) / 2 = -1.666
168
+ expect ( left ) . to . be . closeTo ( - 1.666 , 0.1 ) ;
169
+ // Height matches testHeight, so centered at testTop
170
+ expect ( top ) . to . equal ( 0 ) ;
156
171
} ) ;
157
172
} ) ;
158
173
} ) ;
0 commit comments