Skip to content

Refactor code #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: ws/rewrite-core
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

58 changes: 50 additions & 8 deletions packages/image-comparison-core/src/commands/saveAppElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,42 @@ vi.mock('../helpers/afterScreenshot.js', () => ({
fileName: 'test-element.png'
})
}))
vi.mock('../helpers/options.js', () => ({
buildAfterScreenshotOptions: vi.fn().mockReturnValue({
actualFolder: '/path/to/actual',
base64Image: 'base64-screenshot-data',
filePath: {
browserName: 'chrome',
deviceName: '',
isMobile: false,
savePerInstance: false
},
fileName: {
browserName: 'chrome',
browserVersion: 'latest',
deviceName: '',
devicePixelRatio: 1,
formatImageName: '{tag}',
isMobile: false,
isTestInBrowser: false,
logName: 'chrome',
name: '',
platformName: 'Windows',
platformVersion: 'latest',
screenHeight: 720,
screenWidth: 1366,
tag: 'test-element'
},
isNativeContext: true,
isLandscape: false,
platformName: 'Windows'
})
}))

describe('saveAppElement', () => {
let takeBase64ElementScreenshotSpy: ReturnType<typeof vi.fn>
let afterScreenshotSpy: ReturnType<typeof vi.fn>
let buildAfterScreenshotOptionsSpy: ReturnType<typeof vi.fn>

const baseOptions: InternalSaveElementMethodOptions = {
element: {
Expand All @@ -44,9 +76,11 @@ describe('saveAppElement', () => {
beforeEach(async () => {
const { takeBase64ElementScreenshot } = await import('../methods/images.js')
const afterScreenshot = (await import('../helpers/afterScreenshot.js')).default
const { buildAfterScreenshotOptions } = await import('../helpers/options.js')

takeBase64ElementScreenshotSpy = vi.mocked(takeBase64ElementScreenshot)
afterScreenshotSpy = vi.mocked(afterScreenshot)
buildAfterScreenshotOptionsSpy = vi.mocked(buildAfterScreenshotOptions)
})

afterEach(() => {
Expand All @@ -58,7 +92,8 @@ describe('saveAppElement', () => {

expect(result).toMatchSnapshot()
expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle custom resize dimensions', async () => {
Expand All @@ -81,7 +116,8 @@ describe('saveAppElement', () => {
await saveAppElement(options)

expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle iOS device correctly', async () => {
Expand All @@ -105,7 +141,8 @@ describe('saveAppElement', () => {
await saveAppElement(options)

expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle Android device correctly', async () => {
Expand All @@ -129,7 +166,8 @@ describe('saveAppElement', () => {
await saveAppElement(options)

expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle non-native context correctly', async () => {
Expand All @@ -140,7 +178,8 @@ describe('saveAppElement', () => {
await saveAppElement(options)

expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle custom image naming', async () => {
Expand All @@ -150,7 +189,8 @@ describe('saveAppElement', () => {

await saveAppElement(options)

expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle save per instance', async () => {
Expand All @@ -166,7 +206,8 @@ describe('saveAppElement', () => {

await saveAppElement(options)

expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})

it('should handle custom screen sizes', async () => {
Expand All @@ -186,6 +227,7 @@ describe('saveAppElement', () => {
await saveAppElement(options)

expect(takeBase64ElementScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0][1]).toMatchSnapshot()
})
})
53 changes: 11 additions & 42 deletions packages/image-comparison-core/src/commands/saveAppElement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AfterScreenshotOptions, ScreenshotOutput } from '../helpers/afterScreenshot.interfaces.js'
import type { ScreenshotOutput } from '../helpers/afterScreenshot.interfaces.js'
import afterScreenshot from '../helpers/afterScreenshot.js'
import { DEFAULT_RESIZE_DIMENSIONS } from '../helpers/constants.js'
import { buildAfterScreenshotOptions } from '../helpers/options.js'
import type { ResizeDimensions } from '../methods/images.interfaces.js'
import { takeBase64ElementScreenshot } from '../methods/images.js'
import type { WicElement } from './element.interfaces.js'
Expand All @@ -21,22 +22,10 @@ export default async function saveAppElement(
}: InternalSaveElementMethodOptions
): Promise<ScreenshotOutput> {
// 1. Set some variables
const {
formatImageName,
savePerInstance,
} = saveElementOptions.wic
const resizeDimensions: ResizeDimensions = saveElementOptions.method.resizeDimensions || DEFAULT_RESIZE_DIMENSIONS
const {
browserName,
browserVersion,
deviceName,
devicePixelRatio,
deviceRectangles:{ screenSize },
isIOS,
isMobile,
logName,
platformName,
platformVersion,
} = instanceData

// 2. Take the screenshot
Expand All @@ -48,37 +37,17 @@ export default async function saveAppElement(
resizeDimensions,
})

// 3. The after the screenshot methods
const afterOptions: AfterScreenshotOptions = {
actualFolder: folders.actualFolder,
// 3. The after the screenshot methods
const afterOptions = buildAfterScreenshotOptions({
base64Image,
filePath: {
browserName,
deviceName,
isMobile,
savePerInstance,
},
fileName: {
browserName,
browserVersion,
deviceName,
devicePixelRatio: devicePixelRatio,
formatImageName,
isMobile,
isTestInBrowser: !isNativeContext,
logName,
name: '',
platformName,
platformVersion,
screenHeight: screenSize.height,
screenWidth: screenSize.width,
tag,
},
folders,
tag,
isNativeContext,
isLandscape:false,
platformName,
}
instanceData: instanceData as any, // Use instanceData as enrichedInstanceData for app commands
enrichedInstanceData: instanceData as any,
wicOptions: saveElementOptions.wic
})

// 4. Return the data
// 4. Return the data
return afterScreenshot(browserInstance, afterOptions)
}
43 changes: 43 additions & 0 deletions packages/image-comparison-core/src/commands/saveAppScreen.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import saveAppScreen from './saveAppScreen.js'
import { buildAfterScreenshotOptions } from '../helpers/options.js'
import type { InternalSaveScreenMethodOptions } from './save.interfaces.js'
import {
BASE_CHECK_OPTIONS,
Expand All @@ -20,11 +21,45 @@ vi.mock('../helpers/afterScreenshot.js', () => ({
fileName: 'test-screen.png'
})
}))
vi.mock('../helpers/options.js', () => ({
buildAfterScreenshotOptions: vi.fn().mockReturnValue({
actualFolder: '/test/actual',
base64Image: 'base64-screenshot-data',
filePath: {
browserName: 'test-browser',
deviceName: 'test-device',
isMobile: true,
savePerInstance: false,
},
fileName: {
browserName: 'test-browser',
browserVersion: '17.0',
deviceName: 'test-device',
devicePixelRatio: 2,
formatImageName: '{tag}-{logName}-{width}x{height}-dpr-{dpr}',
isMobile: true,
isTestInBrowser: false,
logName: 'test-log',
name: 'test-device',
outerHeight: NaN,
outerWidth: NaN,
platformName: 'iOS',
platformVersion: '17.0',
screenHeight: 812,
screenWidth: 375,
tag: 'test-screen',
},
isNativeContext: true,
isLandscape: false,
platformName: 'iOS',
})
}))

describe('saveAppScreen', () => {
let takeBase64ScreenshotSpy: ReturnType<typeof vi.fn>
let makeCroppedBase64ImageSpy: ReturnType<typeof vi.fn>
let afterScreenshotSpy: ReturnType<typeof vi.fn>
let buildAfterScreenshotOptionsSpy: ReturnType<typeof vi.fn>

const baseOptions = {
browserInstance: { isAndroid: false, isMobile: false } as any,
Expand Down Expand Up @@ -65,6 +100,7 @@ describe('saveAppScreen', () => {
takeBase64ScreenshotSpy = vi.mocked(takeBase64Screenshot)
makeCroppedBase64ImageSpy = vi.mocked(makeCroppedBase64Image)
afterScreenshotSpy = vi.mocked(afterScreenshot)
buildAfterScreenshotOptionsSpy = vi.mocked(buildAfterScreenshotOptions)
})

afterEach(() => {
Expand All @@ -76,6 +112,7 @@ describe('saveAppScreen', () => {

expect(result).toMatchSnapshot()
expect(takeBase64ScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(makeCroppedBase64ImageSpy).not.toHaveBeenCalled()
})
Expand Down Expand Up @@ -111,6 +148,7 @@ describe('saveAppScreen', () => {
await saveAppScreen(options)

expect(takeBase64ScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(makeCroppedBase64ImageSpy.mock.calls[0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})
Expand Down Expand Up @@ -139,6 +177,7 @@ describe('saveAppScreen', () => {
await saveAppScreen(options)

expect(takeBase64ScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(makeCroppedBase64ImageSpy).not.toHaveBeenCalled()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})
Expand All @@ -151,6 +190,7 @@ describe('saveAppScreen', () => {
await saveAppScreen(options)

expect(takeBase64ScreenshotSpy.mock.calls[0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(makeCroppedBase64ImageSpy).not.toHaveBeenCalled()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})
Expand All @@ -168,6 +208,7 @@ describe('saveAppScreen', () => {

await saveAppScreen(options)

expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})

Expand All @@ -184,6 +225,7 @@ describe('saveAppScreen', () => {

await saveAppScreen(options)

expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})

Expand All @@ -204,6 +246,7 @@ describe('saveAppScreen', () => {
await saveAppScreen(options)

expect(takeBase64ScreenshotSpy.mock.calls[0][0]).toMatchSnapshot()
expect(buildAfterScreenshotOptionsSpy.mock.calls[0][0]).toMatchSnapshot()
expect(afterScreenshotSpy.mock.calls[0]).toMatchSnapshot()
})
})
Loading
Loading