Skip to content

Commit 96e3518

Browse files
committed
Fix #812 - Add support for chaining withClientHints() & withFeatureCheck()
1 parent f1b9a12 commit 96e3518

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

src/main/ua-parser.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,25 +1143,25 @@
11431143
};
11441144
}
11451145

1146-
if (!NAVIGATOR_UADATA) {
1147-
IData.prototype.then = function (cb) {
1148-
var that = this;
1149-
var IDataResolve = function () {
1150-
for (var prop in that) {
1151-
if (that.hasOwnProperty(prop)) {
1152-
this[prop] = that[prop];
1153-
}
1146+
IData.prototype.then = function (cb) {
1147+
var that = this;
1148+
var IDataResolve = function () {
1149+
for (var prop in that) {
1150+
if (that.hasOwnProperty(prop)) {
1151+
this[prop] = that[prop];
11541152
}
1155-
};
1156-
IDataResolve.prototype = {
1157-
is : IData.prototype.is,
1158-
toString : IData.prototype.toString
1159-
};
1160-
var resolveData = new IDataResolve();
1161-
cb(resolveData);
1162-
return resolveData;
1153+
}
11631154
};
1164-
}
1155+
IDataResolve.prototype = {
1156+
is : IData.prototype.is,
1157+
toString : IData.prototype.toString,
1158+
withClientHints : IData.prototype.withClientHints,
1159+
withFeatureCheck : IData.prototype.withFeatureCheck
1160+
};
1161+
var resolveData = new IDataResolve();
1162+
cb(resolveData);
1163+
return resolveData;
1164+
};
11651165

11661166
return new IData();
11671167
};

test/e2e/browser.spec.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,45 @@ test.describe('request.headers can be passed in form of a Headers object', () =>
152152
expect(uap.ua).toBe('myBrowser/1.0');
153153
});
154154
});
155+
156+
test.describe('Chaining withFeatureCheck() & withClientHints() in client-side development', () => {
157+
158+
test('Chain', async ({ page, browserName }) => {
159+
await page.addInitScript((browserName) => {
160+
Object.defineProperty(navigator, 'userAgent', {
161+
value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15'
162+
});
163+
Object.defineProperty(navigator, 'standalone', {
164+
value: true
165+
});
166+
Object.defineProperty(navigator, 'maxTouchPoints', {
167+
value: 3
168+
});
169+
if (browserName == 'chromium') {
170+
Object.defineProperty(navigator, 'userAgentData', {
171+
value: {
172+
brands: [],
173+
platform: '',
174+
mobile: false,
175+
getHighEntropyValues: () => {
176+
return Promise.resolve({
177+
formFactors: 'VR'
178+
});
179+
}
180+
}
181+
});
182+
}
183+
}, browserName);
184+
await page.goto(localHtml);
185+
// @ts-ignore
186+
const fc2ch = await page.evaluate(async () => await UAParser().withFeatureCheck().then(res => res.withClientHints()));
187+
const ch2fc = await page.evaluate(async () => await UAParser().withClientHints().then(res => res.withFeatureCheck()));
188+
if (browserName == 'chromium') {
189+
expect(fc2ch).toHaveProperty('device.type', 'xr'); // overwrite by client hints
190+
expect(ch2fc).toHaveProperty('device.type', 'tablet'); // overwrite by feature check
191+
} else {
192+
expect(fc2ch).toHaveProperty('device.type', 'tablet'); // no client hints found
193+
expect(ch2fc).toHaveProperty('device.type', 'tablet');
194+
}
195+
});
196+
});

test/unit/ua-ch.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,20 @@ describe('Identify vendor & type of device from given model name', () => {
413413
assert.strictEqual(device.type, test.expect.type);
414414
});
415415
});
416+
});
417+
418+
describe('Chaining withClientHints() & withFeatureCheck() in server-side development', () => {
419+
const headers = {
420+
'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
421+
'sec-ch-ua-form-factors' : '"VR"'
422+
};
423+
const device = new UAParser(headers).getDevice();
424+
it('Chain order: withFeatureCheck().withClientHints()', () => {
425+
const fc2ch = device.withFeatureCheck().withClientHints();
426+
assert.strictEqual(fc2ch.type, "xr");
427+
});
428+
it('Chain order: withClientHints().withFeatureCheck()', () => {
429+
const ch2fc = device.withClientHints().withFeatureCheck();
430+
assert.strictEqual(ch2fc.type, "xr");
431+
});
416432
});

0 commit comments

Comments
 (0)