Skip to content

Commit

Permalink
Added normalization function to make options case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
bs-shobhitkumar committed Feb 19, 2025
1 parent 3ecf896 commit 15529fe
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/core/src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,41 @@ function getSnapshotOptions(options, { config, meta }) {
}
});
}
const optionMappings = {
'name': 'name',

Check failure on line 169 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 2 spaces but found 4

Check failure on line 169 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'name' found
'widths': 'widths',

Check failure on line 170 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 2 spaces but found 4

Check failure on line 170 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'widths' found
'scope': 'scope',

Check failure on line 171 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 2 spaces but found 4

Check failure on line 171 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'scope' found
'scopeoptions': 'scopeOptions',

Check failure on line 172 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 2 spaces but found 4

Check failure on line 172 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'scopeoptions' found
'minheight': 'minHeight',

Check failure on line 173 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 2 spaces but found 4

Check failure on line 173 in packages/core/src/snapshot.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'minheight' found
'enablejavascript': 'enableJavaScript',
'enablelayout': 'enableLayout',
'clientinfo': 'clientInfo',
'environmentinfo': 'environmentInfo',
'sync': 'sync',
'testcase': 'testCase',
'labels': 'labels',
'thtestcaseexecutionid': 'thTestCaseExecutionId',
'resources': 'resources',
'meta': 'meta',
'snapshot': 'snapshot',
};

function normalizeOptions(options) {
const normalizedOptions = {};

for (const key in options) {
const lowerCaseKey = key.toLowerCase().replace(/[-_]/g, '');
const normalizedKey = optionMappings[lowerCaseKey] ? optionMappings[lowerCaseKey] : key;
normalizedOptions[normalizedKey] = options[key];
}

return normalizedOptions;
}
// Validates and migrates snapshot options against the correct schema based on provided
// properties. Eagerly throws an error when missing a URL for any snapshot, and warns about all
// other invalid options which are also scrubbed from the returned migrated options.
export function validateSnapshotOptions(options) {
let log = logger('core:snapshot');

// decide which schema to validate against
let schema = (
(['domSnapshot', 'dom-snapshot', 'dom_snapshot']
Expand All @@ -182,6 +210,8 @@ export function validateSnapshotOptions(options) {
('snapshots' in options && '/snapshot/list') ||
('/snapshot'));

options = normalizeOptions(options);

let {
// normalize, migrate, and remove certain properties from validating
clientInfo, environmentInfo, snapshots, ...migrated
Expand Down Expand Up @@ -213,10 +243,8 @@ export function validateSnapshotOptions(options) {
log.warn('Encountered snapshot serialization warnings:');
for (let w of domWarnings) log.warn(`- ${w}`);
}

// warn on validation errors
let errors = PercyConfig.validate(migrated, schema);

if (errors?.length > 0) {
log.warn('Invalid snapshot options:');
for (let e of errors) log.warn(`- ${e.path}: ${e.message}`);
Expand Down
103 changes: 103 additions & 0 deletions packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { generatePromise, AbortController, base64encode } from '../src/utils.js'
import Percy from '@percy/core';
import Pako from 'pako';
import DetectProxy from '@percy/client/detect-proxy';
import { validateSnapshotOptions } from '../src/snapshot.js';

describe('Percy', () => {
let percy, server;
Expand Down Expand Up @@ -1713,4 +1714,106 @@ describe('Percy', () => {
});
});
});
describe('#validateSnapshotOptions', () => {
it('normalizes snapshot options to camelCase and does not produce errors', async () => {
const options = {
name: 'Snapshot 1',
url: 'http://localhost:8000',
'scope-options': { scroll: true },
'min-height': 1024,
'enable-javascript': true,
'client-info': 'client-info',
'environment-info': 'env-info',
'test-case': 'testCase',
'th-test-case-execution-id': '12345',
};

const result = validateSnapshotOptions(options);
expect(result).toEqual({
clientInfo: 'client-info',
environmentInfo: 'env-info',
name: 'Snapshot 1',
url: 'http://localhost:8000',
scopeOptions: { scroll: true },
minHeight: 1024,
enableJavaScript: true,
testCase: 'testCase',
thTestCaseExecutionId: '12345',
});
});

it('handles different casing formats and does not produce errors', async () => {
const options = {
Name: 'Snapshot 1',
url: 'http://localhost:8000',
WIDTHS: [375, 1280],
Scope: '#app',
ScopeOptions: { scroll: true },
MinHeight: 1024,
EnableJavaScript: true,
EnableLayout: false,
ClientInfo: 'client-info',
EnvironmentInfo: 'env-info',
Sync: true,
TestCase: 'testCase',
Labels: 'label1',
ThTestCaseExecutionId: '12345',
};

const result = validateSnapshotOptions(options);
expect(result).toEqual({
clientInfo: 'client-info',
environmentInfo: 'env-info',
name: 'Snapshot 1',
url: 'http://localhost:8000',
widths: [375, 1280],
scope: '#app',
scopeOptions: { scroll: true },
minHeight: 1024,
enableJavaScript: true,
enableLayout: false,
sync: true,
testCase: 'testCase',
labels: 'label1',
thTestCaseExecutionId: '12345',
});
});

it('handles mixed casing and special characters and does not produce errors', async () => {
const options = {
nAmE: 'Snapshot 1',
url: 'http://localhost:8000',
WiDtHs: [375, 1280],
sCoPe: '#app',
sCoPeOpTiOnS: { scroll: true },
mInHeIgHt: 1024,
eNaBlEJaVaScRiPt: true,
eNaBlELaYoUt: false,
cLiEnTInFo: 'client-info',
eNvIrOnMeNtInFo: 'env-info',
sYnC: true,
tEsTcAsE: 'testCase',
lAbElS: 'label1',
tHtEsTcAsEeXeCuTiOnId: '12345',
};

const result = validateSnapshotOptions(options);
expect(result).toEqual({
clientInfo: 'client-info',
environmentInfo: 'env-info',
name: 'Snapshot 1',
url: 'http://localhost:8000',
widths: [375, 1280],
scope: '#app',
scopeOptions: { scroll: true },
minHeight: 1024,
enableJavaScript: true,
enableLayout: false,
sync: true,
testCase: 'testCase',
labels: 'label1',
thTestCaseExecutionId: '12345',
});
});
});
});

0 comments on commit 15529fe

Please sign in to comment.