Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjwala committed Sep 14, 2023
1 parent 1004bff commit 40f14c9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 59 deletions.
3 changes: 2 additions & 1 deletion packages/config/src/utils/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getSchema } from '../validate.js';
const CAMELCASE_MAP = new Map([
['css', 'CSS'],
['javascript', 'JavaScript'],
['dom', 'DOM']
['dom', 'DOM'],
['xpaths', 'XPaths']
]);

// Regular expression that matches words from boundaries or consecutive casing
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ export function createPercyServer(percy, port) {
success: await percy.flush(req.body).then(() => true)
}))
.route('post', '/percy/automateScreenshot', async (req, res) => {
req = percyAutomateRequestHandler(req, percy);
res.json(200, {
success: await (percy.upload(await new WebdriverUtils(req.body).automateScreenshot())).then(() => true)
});
percyAutomateRequestHandler(req, percy);
percy.upload(await new WebdriverUtils(req.body).automateScreenshot());
res.json(200, { success: true });
})
// stops percy at the end of the current event loop
.route('/percy/stop', (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function normalizeURL(url) {
return `${protocol}//${host}${pathname}${search}`;
}

/* istanbul ignore next: tested, but coverage is stripped */
// Returns the body for automateScreenshot in structure
export function percyAutomateRequestHandler(req, percy) {
if (req.body.client_info) {
Expand Down Expand Up @@ -54,8 +55,8 @@ export function percyAutomateRequestHandler(req, percy) {
return [path, [prev, next].filter(Boolean).join('\n')];
}
});

req.body.buildInfo = percy.build;
// returning for testing purpose.
return req;
}

Expand Down
89 changes: 77 additions & 12 deletions packages/core/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import PercyConfig from '@percy/config';
import { logger, setupTest, fs } from './helpers/index.js';
import Percy from '@percy/core';
import WebdriverUtils from '@percy/webdriver-utils'; // eslint-disable-line import/no-extraneous-dependencies
import WebdriverUtils from '@percy/webdriver-utils';

describe('API Server', () => {
let percy;
Expand Down Expand Up @@ -131,17 +131,6 @@ describe('API Server', () => {
});
});

it('has a /automateScreenshot endpoint that calls #upload()', async () => {
spyOn(percy, 'upload').and.resolveTo();
spyOn(WebdriverUtils.prototype, 'automateScreenshot').and.resolveTo(true);
await percy.start();

await expectAsync(request('/percy/automateScreenshot', {
body: { name: 'Snapshot name' },
method: 'post'
})).toBeResolvedTo({ success: true });
});

it('has a /stop endpoint that calls #stop()', async () => {
spyOn(percy, 'stop').and.resolveTo();
await percy.start();
Expand Down Expand Up @@ -277,6 +266,82 @@ describe('API Server', () => {
await expectAsync(pending).toBeResolved();
});

// describe('percyAutomateRequestHandler', () => {
// let req;
// let percyBuildInfo;
// beforeAll(() => {
// req = {
// body: {
// name: 'abc',
// client_info: 'client',
// environment_info: 'environment'
// }
// };

// percyBuildInfo = {
// id: '123',
// url: 'https://percy.io/abc/123'
// };
// });

// it('converts client_info to clientInfo', () => {
// const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
// expect(nreq.body.clientInfo).toBe('client');
// });

// it('converts environment_info to environmentInfo', () => {
// const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
// expect(nreq.body.environmentInfo).toBe('environment');
// });

// it('adds options', () => {
// const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
// expect(nreq.body.options).toEqual({});
// });

// it('adds percyBuildInfo', () => {
// const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
// expect(nreq.body.buildInfo).toEqual(percyBuildInfo);
// });
// });

fit('has a /automateScreenshot endpoint that calls #upload() async with provided options', async () => {
let resolve, test = new Promise(r => (resolve = r));
spyOn(percy, 'upload').and.returnValue(test);
let mockWebdriverUtilResponse = 'TODO: mocked response';
// let percyAutomateRequestHandlerSpy = spyOn(utils, 'percyAutomateRequestHandler').and.callThrough();
spyOn(WebdriverUtils.prototype, 'automateScreenshot').and.resolveTo(mockWebdriverUtilResponse);

await percy.start();

percy.config.snapshot.percyCSS = '.global { color: blue }';
percy.config.snapshot.freezeAnimation = false;
percy.config.snapshot.ignoreRegions = { ignoreRegionSelectors: ['.selector-global'] };
percy.config.snapshot.considerRegions = { considerRegionXPaths: ['/xpath-global'] };

await expectAsync(request('/percy/automateScreenshot', {
body: {
name: 'Snapshot name',
client_info: 'client',
environment_info: 'environment',
options: {
percyCSS: '.percy-screenshot: { color: red }',
freeze_animation: true,
ignore_region_xpaths: ['/xpath-per-screenshot'],
consider_region_xpaths: ['/xpath-per-screenshot']
}
},
method: 'post'
})).toBeResolvedTo({ success: true });

// expect(percyAutomateRequestHandlerSpy).toHaveBeenCalledOnceWith({

// })
expect(percy.upload).toHaveBeenCalledOnceWith(mockWebdriverUtilResponse);
await expectAsync(test).toBePending();
resolve(); // no hanging promises
});

it('returns a 500 error when an endpoint throws', async () => {
spyOn(percy, 'snapshot').and.rejectWith(new Error('test error'));
await percy.start();
Expand Down
42 changes: 1 addition & 41 deletions packages/core/test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
generatePromise,
AbortController,
yieldTo,
yieldAll,
percyAutomateRequestHandler
yieldAll
} from '../../src/utils.js';

describe('Unit / Utils', () => {
Expand Down Expand Up @@ -166,43 +165,4 @@ describe('Unit / Utils', () => {
{ done: true, value: [2, 4, null, 3, 6] });
});
});

describe('percyAutomateRequestHandler', () => {
let req;
let percyBuildInfo;
beforeAll(() => {
req = {
body: {
name: 'abc',
client_info: 'client',
environment_info: 'environment'
}
};

percyBuildInfo = {
id: '123',
url: 'https://percy.io/abc/123'
};
});

it('converts client_info to clientInfo', () => {
const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
expect(nreq.body.clientInfo).toBe('client');
});

it('converts environment_info to environmentInfo', () => {
const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
expect(nreq.body.environmentInfo).toBe('environment');
});

it('adds options', () => {
const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
expect(nreq.body.options).toEqual({});
});

it('adds percyBuildInfo', () => {
const nreq = percyAutomateRequestHandler(req, percyBuildInfo);
expect(nreq.body.buildInfo).toEqual(percyBuildInfo);
});
});
});

0 comments on commit 40f14c9

Please sign in to comment.