Skip to content

Commit

Permalink
Create test for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Nov 10, 2023
1 parent 7abc8b9 commit 106d882
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/helpers/ua-parser-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ const isChromiumBased = (res) => res.engine.is(Engine.BLINK);
module.exports = {
isAppleSilicon,
isChromiumBased
}

// TODO: create test
}
8 changes: 7 additions & 1 deletion test/dts-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectType } from 'tsd';
import { UAParser, IResult, IBrowser, ICPU, IEngine, IDevice, IOS } from "../src/main/ua-parser";
import { isAppleSilicon, isChromiumBased } from "../src/helpers/ua-parser-helpers";

const uastring = 'Mozilla/5.0 (X11; MyCustomOS; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0';
const extensions = {
Expand Down Expand Up @@ -39,4 +40,9 @@ expectType<IEngine>(parser.getEngine());
expectType<IOS>(parser.getOS());
expectType<IResult>(parser.getResult());
expectType<string>(parser.getUA());
expectType<UAParser>(parser.setUA(uastring));
expectType<UAParser>(parser.setUA(uastring));

const result = parser.getResult();

expectType<boolean>(isAppleSilicon(result));
expectType<boolean>(isChromiumBased(result));
26 changes: 26 additions & 0 deletions test/mocha-test-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const assert = require('assert');
const UAParser = require('ua-parser-js');
const { isAppleSilicon, isChromiumBased } = require('ua-parser-js/helpers');

describe('isAppleSilicon', () => {
it('Can detect Apple Silicon device', () => {

// non-real ua
const macARM = 'Mozilla/5.0 (Macintosh; ARM; Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0';
const macIntel = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0';

assert.equal(isAppleSilicon(UAParser(macIntel)), false);
assert.equal(isAppleSilicon(UAParser(macARM)), true);
});
});

describe('isChromiumBased', () => {
it('Can detect Chromium-based browser', () => {

const edge = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.2151.58';
const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0';

assert.equal(isChromiumBased(UAParser(edge)), true);
assert.equal(isChromiumBased(UAParser(firefox)), false);
});
});

0 comments on commit 106d882

Please sign in to comment.