Skip to content

Commit a7ec171

Browse files
Refactor code (#987)
* feat: refactor getBase64FullPageScreenshotsData router - update ut's * chore: update tests * chore: refactor beforeOptions - update saveFullPageScreen and tests * feat: adjust saveWebScreen with createBeforeScreenshotOptions * feat: adjust saveWebElement for createBeforeScreenshotOptions * chore: optimise saveWebElement * chore: restructure interface * chore: refactor afterScreenshot options - implemented for saveWebScreen * chore: refactor for saveWebElement * chore: refactor saveFullPageScreen * chore: refactor saveAppScreen * chore: refactor saveAppElement * chore: refactor saveWebScreen * chore: refactor screenshot interfaces * chore: and the rest * chore: use snapshots for tests * fix: fallback for elementscreeshots was broken due to incorrect calculation, also fixed UT's * chore: fix local desktop run * chore: fix some comments * chore: code optimisation - afterscreenshot for readability * chore: optimisations - make takeElementScreenshot more readable - fix UT for afterscreenshot - add check on calling logger for afterscreenshot * chore: then also commit the afterscreenshot stuff * chore: refactor a CommonCheckVariables - adjust files - create UT's * chore: refactor to use buildFolderOptions - adjust methods - add and adjust UTs * chore: refactor methods options - added buildBaseExecuteCompareOptions - added and adjusted UT's * chore: refactor checkAppElement and checkAppScreen * chore: refactored images and report code - updated interfaces - made code more dry and maintainable - updated UTs * chore: fix linting * chore: fix build * Chore: remove comments
1 parent 37bb47a commit a7ec171

84 files changed

Lines changed: 10000 additions & 4396 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eslint.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = [
77
'**/dist/*',
88
'**/build/*',
99
'**/.next/*',
10+
'**/.vitest-ui/*',
1011
]
1112
},
1213
{

packages/image-comparison-core/src/base.interfaces.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ export interface Folders {
77
diffFolder: string;
88
}
99

10+
export interface FolderPaths {
11+
/** The actual folder path where the current screenshots need to be saved */
12+
actualFolderPath: string;
13+
/** The baseline folder path where the baseline screenshots can be found */
14+
baselineFolderPath: string;
15+
/** The diff folder path where the differences are saved */
16+
diffFolderPath: string;
17+
}
18+
19+
export interface FilePaths {
20+
/** The actual file path where the current screenshots need to be saved */
21+
actualFilePath: string;
22+
/** The baseline file path where the baseline screenshots can be found */
23+
baselineFilePath: string;
24+
/** The diff file path where the difference is saved */
25+
diffFilePath: string;
26+
}
27+
1028
export interface BaseWebScreenshotOptions {
1129
/**
1230
* Disable the blinking cursor
@@ -188,4 +206,4 @@ export interface BaseBoundingBox {
188206
left: number;
189207
/** The top coordinate */
190208
top: number;
191-
}
209+
}

packages/image-comparison-core/src/base.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { rmSync } from 'node:fs'
33
import BaseClass from './base.js'
44

55
vi.mock('node:fs', () => ({
6-
...vi.importActual('node:fs'), // This includes the actual implementations of other 'fs' methods
7-
rmSync: vi.fn(), // Mock implementation for rmSync
6+
...vi.importActual('node:fs'),
7+
rmSync: vi.fn(),
88
}))
99

1010
describe('BaseClass', () => {

packages/image-comparison-core/src/clientSideScripts/drawTabbableOnCanvas.test.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,19 @@ describe('drawTabbableOnCanvas', () => {
4040
}
4141

4242
beforeEach(() => {
43-
// Reset DOM
4443
document.body.innerHTML = ''
4544

46-
// Mock window properties
4745
Object.defineProperty(window, 'innerWidth', { value: 1024, configurable: true })
4846
Object.defineProperty(window, 'innerHeight', { value: 768, configurable: true })
49-
50-
// Mock document properties
5147
Object.defineProperty(document.documentElement, 'clientHeight', { value: 768, configurable: true })
5248
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 1000, configurable: true })
5349
Object.defineProperty(document.body, 'scrollHeight', { value: 1000, configurable: true })
54-
55-
// Mock window.scrollTo
5650
window.scrollTo = vi.fn()
5751

58-
// Mock canvas context
5952
const mockGetContext = vi.fn().mockReturnValue(mockCanvasContext)
53+
6054
HTMLCanvasElement.prototype.getContext = mockGetContext
6155

62-
// Reset all mock functions
6356
vi.clearAllMocks()
6457
})
6558

@@ -77,7 +70,6 @@ describe('drawTabbableOnCanvas', () => {
7770
})
7871

7972
it('should draw lines and circles for tabbable elements', () => {
80-
// Create some tabbable elements
8173
const button = document.createElement('button')
8274
button.textContent = 'Test Button'
8375
button.tabIndex = 0
@@ -88,7 +80,6 @@ describe('drawTabbableOnCanvas', () => {
8880
input.tabIndex = 0
8981
document.body.appendChild(input)
9082

91-
// Mock getBoundingClientRect for the elements
9283
const mockRect = {
9384
left: 100,
9485
top: 100,
@@ -99,11 +90,9 @@ describe('drawTabbableOnCanvas', () => {
9990
}
10091
Element.prototype.getBoundingClientRect = vi.fn().mockReturnValue(mockRect)
10192

102-
// Mock offsetParent to make elements visible
10393
Object.defineProperty(button, 'offsetParent', { value: document.body, configurable: true })
10494
Object.defineProperty(input, 'offsetParent', { value: document.body, configurable: true })
10595

106-
// Create spies for canvas context methods
10796
const beginPathSpy = vi.spyOn(mockCanvasContext, 'beginPath')
10897
const globalCompositeOperationSpy = vi.spyOn(mockCanvasContext, 'globalCompositeOperation', 'set')
10998
const fillStyleSpy = vi.spyOn(mockCanvasContext, 'fillStyle', 'set')
@@ -112,27 +101,21 @@ describe('drawTabbableOnCanvas', () => {
112101

113102
drawTabbableOnCanvas(defaultOptions)
114103

115-
// Verify the sequence of operations
116104
expect(beginPathSpy).toHaveBeenCalled()
117105

118-
// Check line drawing operations (happens first)
119106
expect(globalCompositeOperationSpy).toHaveBeenNthCalledWith(1, 'destination-over')
120107
expect(lineWidthSpy).toHaveBeenNthCalledWith(1, defaultOptions.line!.width)
121108
expect(strokeStyleSpy).toHaveBeenNthCalledWith(1, defaultOptions.line!.color)
122109
expect(mockCanvasContext.moveTo).toHaveBeenCalled()
123110
expect(mockCanvasContext.lineTo).toHaveBeenCalled()
124111
expect(mockCanvasContext.stroke).toHaveBeenCalled()
125-
126-
// Check circle drawing operations (happens second)
127112
expect(globalCompositeOperationSpy).toHaveBeenNthCalledWith(2, 'source-over')
128113
expect(fillStyleSpy).toHaveBeenNthCalledWith(1, defaultOptions.circle!.backgroundColor)
129114
expect(lineWidthSpy).toHaveBeenNthCalledWith(2, defaultOptions.circle!.borderWidth)
130115
expect(strokeStyleSpy).toHaveBeenNthCalledWith(2, defaultOptions.circle!.borderColor)
131116
expect(mockCanvasContext.arc).toHaveBeenCalled()
132117
expect(mockCanvasContext.fill).toHaveBeenCalled()
133118
expect(mockCanvasContext.stroke).toHaveBeenCalled()
134-
135-
// Check number drawing operations (happens last)
136119
expect(fillStyleSpy).toHaveBeenNthCalledWith(2, defaultOptions.circle!.fontColor)
137120
expect(mockCanvasContext.font).toBe(`${defaultOptions.circle!.fontSize}px ${defaultOptions.circle!.fontFamily}`)
138121
expect(mockCanvasContext.textAlign).toBe('center')
@@ -149,7 +132,6 @@ describe('drawTabbableOnCanvas', () => {
149132
})
150133

151134
it('should handle hidden elements', () => {
152-
// Create a hidden button
153135
const button = document.createElement('button')
154136
button.style.visibility = 'hidden'
155137
document.body.appendChild(button)
@@ -160,7 +142,6 @@ describe('drawTabbableOnCanvas', () => {
160142
})
161143

162144
it('should handle disabled elements', () => {
163-
// Create a disabled button
164145
const button = document.createElement('button')
165146
button.disabled = true
166147
document.body.appendChild(button)
@@ -199,17 +180,18 @@ describe('drawTabbableOnCanvas', () => {
199180
radio1.type = 'radio'
200181
radio1.name = 'group1'
201182
document.body.appendChild(radio1)
183+
202184
const radio2 = document.createElement('input')
203185
radio2.type = 'radio'
204186
radio2.name = 'group1'
205187
radio2.checked = true
206188
document.body.appendChild(radio2)
207-
// Make radios visible
208189
Object.defineProperty(radio1, 'offsetParent', { value: document.body, configurable: true })
209190
Object.defineProperty(radio2, 'offsetParent', { value: document.body, configurable: true })
210-
// Mock getBoundingClientRect
211191
radio2.getBoundingClientRect = vi.fn().mockReturnValue({ left: 0, top: 0, width: 10, height: 10, right: 10, bottom: 10 })
192+
212193
drawTabbableOnCanvas(defaultOptions)
194+
213195
expect(mockCanvasContext.beginPath).toHaveBeenCalled()
214196
})
215197

@@ -253,14 +235,14 @@ describe('drawTabbableOnCanvas', () => {
253235
Object.defineProperty(document.documentElement, 'clientHeight', { value: 100, configurable: true })
254236
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 100, configurable: true })
255237
Object.defineProperty(document.body, 'scrollHeight', { value: 200, configurable: true })
256-
// Set window.innerHeight to match the others
257238
Object.defineProperty(window, 'innerHeight', { value: 100, configurable: true })
258-
// Add a tabbable element to trigger canvas drawing
239+
259240
const btn = document.createElement('button')
260241
btn.tabIndex = 0
261242
Object.defineProperty(btn, 'offsetParent', { value: document.body, configurable: true })
262243
btn.getBoundingClientRect = vi.fn().mockReturnValue({ left: 0, top: 0, width: 10, height: 10, right: 10, bottom: 10 })
263244
document.body.appendChild(btn)
245+
264246
drawTabbableOnCanvas(defaultOptions)
265247

266248
const canvas = document.getElementById('wic-tabbable-canvas') as HTMLCanvasElement
@@ -272,52 +254,54 @@ describe('drawTabbableOnCanvas', () => {
272254
Object.defineProperty(document.documentElement, 'clientHeight', { value: 100, configurable: true })
273255
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 100, configurable: true })
274256
Object.defineProperty(document.body, 'scrollHeight', { value: 100, configurable: true })
275-
// Add a tall div
257+
276258
const tallDiv = document.createElement('div')
277259
tallDiv.style.height = '300px'
278260
document.body.appendChild(tallDiv)
279261
tallDiv.getBoundingClientRect = vi.fn().mockReturnValue({ top: 0 })
262+
280263
drawTabbableOnCanvas(defaultOptions)
264+
281265
const canvas = document.getElementById('wic-tabbable-canvas') as HTMLCanvasElement
266+
282267
expect(canvas.height).toBeGreaterThanOrEqual(100)
283268
})
284269

285270
it('should not throw or attempt to draw if getContext returns null (drawLine)', () => {
286-
// Mock getContext to return null
287271
const originalGetContext = HTMLCanvasElement.prototype.getContext
288272
HTMLCanvasElement.prototype.getContext = vi.fn().mockReturnValue(null)
289-
// Add two tabbable elements to trigger drawLine
273+
290274
const btn1 = document.createElement('button')
291275
btn1.tabIndex = 0
292276
Object.defineProperty(btn1, 'offsetParent', { value: document.body, configurable: true })
293277
btn1.getBoundingClientRect = vi.fn().mockReturnValue({ left: 0, top: 0, width: 10, height: 10, right: 10, bottom: 10 })
294278
document.body.appendChild(btn1)
279+
295280
const btn2 = document.createElement('button')
296281
btn2.tabIndex = 0
297282
Object.defineProperty(btn2, 'offsetParent', { value: document.body, configurable: true })
298283
btn2.getBoundingClientRect = vi.fn().mockReturnValue({ left: 20, top: 20, width: 10, height: 10, right: 30, bottom: 30 })
299284
document.body.appendChild(btn2)
300-
// Should not throw or call any drawing methods
285+
301286
expect(() => drawTabbableOnCanvas(defaultOptions)).not.toThrow()
302287
expect(mockCanvasContext.beginPath).not.toHaveBeenCalled()
303-
// Restore
288+
304289
HTMLCanvasElement.prototype.getContext = originalGetContext
305290
})
306291

307292
it('should not throw or attempt to draw if getContext returns null (drawCircleAndNumber)', () => {
308-
// Mock getContext to return null
309293
const originalGetContext = HTMLCanvasElement.prototype.getContext
310294
HTMLCanvasElement.prototype.getContext = vi.fn().mockReturnValue(null)
311-
// Add one tabbable element to trigger drawCircleAndNumber
295+
312296
const btn = document.createElement('button')
313297
btn.tabIndex = 0
314298
Object.defineProperty(btn, 'offsetParent', { value: document.body, configurable: true })
315299
btn.getBoundingClientRect = vi.fn().mockReturnValue({ left: 0, top: 0, width: 10, height: 10, right: 10, bottom: 10 })
316300
document.body.appendChild(btn)
317-
// Should not throw or call any drawing methods
301+
318302
expect(() => drawTabbableOnCanvas(defaultOptions)).not.toThrow()
319303
expect(mockCanvasContext.beginPath).not.toHaveBeenCalled()
320-
// Restore
304+
321305
HTMLCanvasElement.prototype.getContext = originalGetContext
322306
})
323307
})

packages/image-comparison-core/src/clientSideScripts/getDocumentScrollHeight.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,27 @@ import { CONFIGURABLE } from '../mocks/mocks.js'
66

77
describe('getDocumentScrollHeight', () => {
88
it('should return the bodyScrollHeight', () => {
9-
// For viewPortHeight
109
Object.defineProperty(document.documentElement, 'clientHeight', { value: 500, ...CONFIGURABLE })
1110
Object.defineProperty(window, 'innerHeight', { value: 500, ...CONFIGURABLE })
12-
// For scrollHeight
1311
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 500, ...CONFIGURABLE })
14-
// For bodyScrollHeight
1512
Object.defineProperty(document.body, 'scrollHeight', { value: 1500, ...CONFIGURABLE })
1613

1714
expect(getDocumentScrollHeight()).toEqual(1500)
1815
})
1916

2017
it('should return the scrollHeight', () => {
21-
// For viewPortHeight
2218
Object.defineProperty(document.documentElement, 'clientHeight', { value: 500, ...CONFIGURABLE })
2319
Object.defineProperty(window, 'innerHeight', { value: 500, ...CONFIGURABLE })
24-
// For scrollHeight
2520
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 2250, ...CONFIGURABLE })
26-
// For bodyScrollHeight
2721
Object.defineProperty(document.body, 'scrollHeight', { value: 1500, ...CONFIGURABLE })
2822

2923
expect(getDocumentScrollHeight()).toEqual(2250)
3024
})
3125

3226
it('should return the height of the largest node', () => {
33-
// For viewPortHeight
3427
Object.defineProperty(document.documentElement, 'clientHeight', { value: 1500, ...CONFIGURABLE })
3528
Object.defineProperty(window, 'innerHeight', { value: 1500, ...CONFIGURABLE })
36-
// For scrollHeight
3729
Object.defineProperty(document.documentElement, 'scrollHeight', { value: 1500, ...CONFIGURABLE })
38-
// For bodyScrollHeight
3930
Object.defineProperty(document.body, 'scrollHeight', { value: 1500, ...CONFIGURABLE })
4031
document.body.innerHTML =
4132
'<div>' + ' <span style="height: 200px;width: 50px"/>' + ' <div style="height: 500px;width: 50px" />' + '</div>'

packages/image-comparison-core/src/clientSideScripts/getScreenDimensions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('getScreenDimensions', () => {
2929
Object.defineProperty(window, 'outerHeight', { value: 0 })
3030
Object.defineProperty(window, 'outerWidth', { value: 0 })
3131
Object.defineProperty(document.documentElement, 'clientHeight', { value: 1234 })
32-
Object.defineProperty(document.documentElement, 'clientWidth', { value: 4321 }) // @ts-ignore
32+
Object.defineProperty(document.documentElement, 'clientWidth', { value: 4321 })
3333
Object.defineProperty(window, 'matchMedia', {
3434
value: vi.fn().mockImplementation(() => ({
3535
matches: false,

0 commit comments

Comments
 (0)