Skip to content

Commit 005be06

Browse files
authored
feat: expose deviceDetails in healthcheck endpoint for responsive capture (#2112)
* feat: add deviceDetails to healthcheck endpoint and SDK - Export complete deviceDetails object from /percy/healthcheck endpoint - Store deviceDetails in percy object in sdk-utils for easy access - Previously only device widths were available, now full device info is accessible * test: add tests for deviceDetails in healthcheck and SDK - Add deviceDetails to expected healthcheck response validation - Test deviceDetails is properly stored in percy object after isPercyEnabled - Add test cases for both empty and populated deviceDetails scenarios - Validate deviceDetails integration in /test/api/config test * fixing tests
1 parent 2fc7e5c commit 005be06

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

packages/core/src/api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export function createPercyServer(percy, port) {
8989
// This will only be used if width is not passed in options
9090
config: percy.config.snapshot.widths
9191
},
92+
deviceDetails: percy.deviceDetails || [],
9293
success: true,
9394
type: percy.client.tokenType()
9495
}))
@@ -216,7 +217,8 @@ export function createPercyServer(percy, port) {
216217
percy.testing.version = body;
217218
} else if (cmd === 'config') {
218219
percy.config.snapshot.widths = body.config;
219-
percy.deviceDetails = body.mobile?.map((w) => { return { width: w }; });
220+
// Support setting deviceDetails directly or deriving from mobile widths
221+
percy.deviceDetails = body.deviceDetails || body.mobile?.map((w) => { return { width: w }; });
220222
percy.config.snapshot.responsiveSnapshotCapture = !!body.responsive;
221223
percy.config.percy.deferUploads = !!body.deferUploads;
222224
} else if (cmd === 'error' || cmd === 'disconnect') {

packages/core/test/api.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('API Server', () => {
6161
loglevel: 'info',
6262
config: PercyConfig.getDefaults(),
6363
widths: { mobile: [], config: PercyConfig.getDefaults().snapshot.widths },
64+
deviceDetails: [],
6465
build: {
6566
id: '123',
6667
number: 1,
@@ -88,7 +89,8 @@ describe('API Server', () => {
8889
widths: {
8990
mobile: [390],
9091
config: [1000]
91-
}
92+
},
93+
deviceDetails: [{ width: 390, devicePixelRatio: 2 }]
9294
}));
9395
});
9496

@@ -741,21 +743,24 @@ describe('API Server', () => {
741743
});
742744

743745
it('can manipulate the config widths via /test/api/config', async () => {
744-
let { widths, config } = await get('/percy/healthcheck');
746+
let { widths, config, deviceDetails } = await get('/percy/healthcheck');
745747
expect(widths.config).toEqual([375, 1280]);
746748
expect(widths.mobile).toEqual([]);
749+
expect(deviceDetails).toEqual([]);
747750

748751
await post('/test/api/config', { config: [390], deferUploads: true });
749-
({ widths, config } = await get('/percy/healthcheck'));
752+
({ widths, config, deviceDetails } = await get('/percy/healthcheck'));
750753
expect(widths.config).toEqual([390]);
751754
expect(config.snapshot.responsiveSnapshotCapture).toEqual(false);
752755
expect(config.percy.deferUploads).toEqual(true);
756+
expect(deviceDetails).toEqual([]);
753757

754758
await post('/test/api/config', { config: [375, 1280], mobile: [456], responsive: true });
755-
({ widths, config } = await get('/percy/healthcheck'));
759+
({ widths, config, deviceDetails } = await get('/percy/healthcheck'));
756760
expect(widths.mobile).toEqual([456]);
757761
expect(config.snapshot.responsiveSnapshotCapture).toEqual(true);
758762
expect(config.percy.deferUploads).toEqual(false);
763+
expect(deviceDetails).toEqual([{ width: 456 }]);
759764
});
760765

761766
it('can make endpoints return server errors via /test/api/error', async () => {

packages/sdk-utils/src/percy-enabled.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function isPercyEnabled() {
1616
percy.enabled = true;
1717
percy.type = response.body.type;
1818
percy.widths = response.body.widths;
19+
percy.deviceDetails = response.body.deviceDetails;
1920
} catch (e) {
2021
percy.enabled = false;
2122
error = e;

packages/sdk-utils/test/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const helpers = {
77
delete utils.percy.build;
88
delete utils.percy.enabled;
99
delete utils.percy.domScript;
10+
delete utils.percy.deviceDetails;
1011
delete utils.logger.log.history;
1112
delete utils.logger.loglevel.lvl;
1213
delete process.env.PERCY_LOGLEVEL;

packages/sdk-utils/test/index.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ describe('SDK Utils', () => {
6666
expect(percy.widths).toHaveProperty('config', [375, 1280]);
6767
expect(percy.widths).toHaveProperty('mobile', []);
6868
});
69+
70+
it('contains percy deviceDetails', () => {
71+
expect(percy.deviceDetails).toEqual([]);
72+
});
6973
});
7074
});
7175

@@ -117,6 +121,25 @@ describe('SDK Utils', () => {
117121
await expectAsync(utils.postSnapshot({})).toBeResolved();
118122
await expectAsync(isPercyEnabled()).toBeResolvedTo(false);
119123
});
124+
125+
it('stores deviceDetails when populated', async () => {
126+
// Set up deviceDetails via test config
127+
await helpers.test('config', { mobile: [390, 456] });
128+
129+
// Reset and refetch healthcheck data
130+
utils.percy.enabled = null;
131+
await expectAsync(isPercyEnabled()).toBeResolvedTo(true);
132+
133+
expect(utils.percy.deviceDetails).toEqual([
134+
{ width: 390 },
135+
{ width: 456 }
136+
]);
137+
138+
// Cleanup: reset deviceDetails back to empty
139+
await helpers.test('config', { config: [375, 1280] });
140+
utils.percy.enabled = null;
141+
utils.percy.deviceDetails = undefined;
142+
});
120143
});
121144

122145
describe('waitForPercyIdle()', () => {

0 commit comments

Comments
 (0)