Skip to content

Commit eefb9e4

Browse files
aded regions support (#929)
* aded regions * lint * test * updated cli to 1.30.9 * resolved comments
1 parent da8d269 commit eefb9e4

File tree

6 files changed

+340
-208
lines changed

6 files changed

+340
-208
lines changed

createRegion.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const createRegion = function({
2+
boundingBox = null,
3+
elementXpath = null,
4+
elementCSS = null,
5+
padding = null,
6+
algorithm = 'ignore',
7+
diffSensitivity = null,
8+
imageIgnoreThreshold = null,
9+
carouselsEnabled = null,
10+
bannersEnabled = null,
11+
adsEnabled = null,
12+
diffIgnoreThreshold = null
13+
} = {}) {
14+
const elementSelector = {};
15+
if (boundingBox) elementSelector.boundingBox = boundingBox;
16+
if (elementXpath) elementSelector.elementXpath = elementXpath;
17+
if (elementCSS) elementSelector.elementCSS = elementCSS;
18+
19+
const region = {
20+
algorithm,
21+
elementSelector
22+
};
23+
24+
if (padding) {
25+
region.padding = padding;
26+
}
27+
28+
const configuration = {};
29+
if (['standard', 'intelliignore'].includes(algorithm)) {
30+
if (diffSensitivity) configuration.diffSensitivity = diffSensitivity;
31+
if (imageIgnoreThreshold) configuration.imageIgnoreThreshold = imageIgnoreThreshold;
32+
if (carouselsEnabled) configuration.carouselsEnabled = carouselsEnabled;
33+
if (bannersEnabled) configuration.bannersEnabled = bannersEnabled;
34+
if (adsEnabled) configuration.adsEnabled = adsEnabled;
35+
}
36+
37+
if (Object.keys(configuration).length > 0) {
38+
region.configuration = configuration;
39+
}
40+
41+
const assertion = {};
42+
if (diffIgnoreThreshold) {
43+
assertion.diffIgnoreThreshold = diffIgnoreThreshold;
44+
}
45+
46+
if (Object.keys(assertion).length > 0) {
47+
region.assertion = assertion;
48+
}
49+
50+
return region;
51+
};
52+
53+
module.exports = { createRegion };

cypress/e2e/index.cy.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import helpers from '@percy/sdk-utils/test/helpers';
2+
import { createRegion } from '../../createRegion';
23

34
const { match } = Cypress.sinon;
45

@@ -198,4 +199,79 @@ describe('percySnapshot', () => {
198199
});
199200
});
200201
});
202+
203+
describe('createRegion function', () => {
204+
it('creates a region object with default values', () => {
205+
const region = createRegion();
206+
expect(region).to.deep.equal({ algorithm: 'ignore', elementSelector: {} });
207+
});
208+
209+
it('creates a region object with provided values', () => {
210+
const region = createRegion({ boundingBox: { x: 10, y: 20, width: 100, height: 200 }, algorithm: 'standard' });
211+
expect(region).to.deep.equal({
212+
algorithm: 'standard',
213+
elementSelector: { boundingBox: { x: 10, y: 20, width: 100, height: 200 } }
214+
});
215+
});
216+
217+
it('adds configuration properties when using standard or intelliignore', () => {
218+
const region = createRegion({ algorithm: 'standard', diffSensitivity: 0.5 });
219+
expect(region).to.have.property('configuration');
220+
expect(region.configuration).to.deep.equal({ diffSensitivity: 0.5 });
221+
});
222+
223+
it('adds assertion properties if diffIgnoreThreshold is provided', () => {
224+
const region = createRegion({ diffIgnoreThreshold: 0.1 });
225+
expect(region).to.have.property('assertion');
226+
expect(region.assertion).to.deep.equal({ diffIgnoreThreshold: 0.1 });
227+
});
228+
229+
it('includes padding when provided', () => {
230+
const region = createRegion({ padding: { top: 10 } });
231+
expect(region).to.have.property('padding').that.deep.equals({ top: 10 });
232+
});
233+
234+
it('includes configuration when algorithm is standard', () => {
235+
const region = createRegion({ algorithm: 'standard', diffSensitivity: 5 });
236+
expect(region.configuration.diffSensitivity).to.equal(5);
237+
});
238+
239+
it('includes configuration when algorithm is intelliignore', () => {
240+
const region = createRegion({ algorithm: 'intelliignore', imageIgnoreThreshold: 0.2 });
241+
expect(region.configuration.imageIgnoreThreshold).to.equal(0.2);
242+
});
243+
244+
it('does not include configuration for ignore algorithm', () => {
245+
const region = createRegion({ algorithm: 'ignore', diffSensitivity: 5 });
246+
expect(region.configuration).to.be.undefined;
247+
});
248+
249+
it('sets elementXpath in elementSelector', () => {
250+
const region = createRegion({ elementXpath: "//div[@id='test']" });
251+
expect(region.elementSelector.elementXpath).to.equal("//div[@id='test']");
252+
});
253+
254+
it('sets elementCSS in elementSelector', () => {
255+
const region = createRegion({ elementCSS: '.test-class' });
256+
expect(region.elementSelector.elementCSS).to.equal('.test-class');
257+
});
258+
});
259+
260+
it('includes carouselsEnabled in configuration if provided', () => {
261+
const region = createRegion({ algorithm: 'standard', carouselsEnabled: true });
262+
expect(region).to.have.property('configuration');
263+
expect(region.configuration).to.have.property('carouselsEnabled', true);
264+
});
265+
266+
it('includes bannersEnabled in configuration if provided', () => {
267+
const region = createRegion({ algorithm: 'standard', bannersEnabled: true });
268+
expect(region).to.have.property('configuration');
269+
expect(region.configuration).to.have.property('bannersEnabled', true);
270+
});
271+
272+
it('includes adsEnabled in configuration if provided', () => {
273+
const region = createRegion({ algorithm: 'standard', adsEnabled: true });
274+
expect(region).to.have.property('configuration');
275+
expect(region.configuration).to.have.property('adsEnabled', true);
276+
});
201277
});

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const utils = require('@percy/sdk-utils');
2+
const { createRegion } = require('./createRegion');
23

34
// Collect client and environment information
45
const sdkPkg = require('./package.json');
@@ -127,3 +128,5 @@ Cypress.Commands.add('percySnapshot', (name, options = {}) => {
127128
});
128129
});
129130
});
131+
132+
module.exports = { createRegion };

0 commit comments

Comments
 (0)