Skip to content

Commit

Permalink
feat(inApp): lightweight detecting mobile on in-app
Browse files Browse the repository at this point in the history
  • Loading branch information
yutin1987 committed Apr 3, 2018
1 parent e230f84 commit b82a67e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 336 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lib"
],
"dependencies": {
"lodash": "^4.17.4"
"lodash": "^4.17.5"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand All @@ -29,20 +29,20 @@
"babel-jest": "^20.0.3",
"babel-polyfill": "^6.23.0",
"babel-preset-react-native": "^1.9.2",
"cross-env": "^5.0.0",
"clipboard": "^1.6.1",
"cross-env": "^5.0.0",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.0.1",
"gh-pages": "^1.0.0",
"jest": "^20.0.3",
"primer-css": "^6.0.0",
"react": "15.5.4",
"react-dom": "15.5.4",
"react-icons": "^2.2.5",
"react-scripts": "1.0.1",
"primer-css": "^6.0.0",
"semantic-release": "^6.3.6"
}
}
86 changes: 9 additions & 77 deletions src/__test__/inapp.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import _ from 'lodash';
import InApp from '../inapp';

// check user agents from https://developers.whatismybrowser.com/

const DESKTOP = {
MACOS: {
CHROME: [
Expand Down Expand Up @@ -79,7 +81,7 @@ const MOBILE = {
],
},
PIXEL: {
puffin: [
PUFFIN: [
'Mozilla/5.0 (Linux; Android 7.1.2; Pixel Build/N2G47E; zh-tw) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Puffin/6.0.9.15863AP',
],
},
Expand Down Expand Up @@ -180,47 +182,6 @@ const TABLET = {
};

describe('InApp', () => {
describe('os', () => {
it('iphone', () => {
_.forEach(MOBILE.IPHONE, useragents => _.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.os).toBe('ios');
}));
});

it('android', () => {
_.forEach(_.concat(
MOBILE.SONY, MOBILE.ASUS, MOBILE.PIXEL, MOBILE.REDMI,
), useragents => _.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.os).toBe('android');
}));
});
});

describe('device', () => {
it('iphone', () => {
_.forEach(MOBILE.IPHONE, useragents => _.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.device).toBe('iphone');
}));
});

it('sony', () => {
_.forEach(MOBILE.SONY, useragents => _.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.device).toBe('sony');
}));
});

it('samsung', () => {
_.forEach(MOBILE.SAMSUNG, useragents => _.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.device).toBe('samsung');
}));
});
});

describe('browser', () => {
it('browser', () => {
_.forEach(MOBILE, device => _.forEach(device, (useragents, name) =>
Expand All @@ -237,7 +198,7 @@ describe('InApp', () => {
_.forEach(Object.assign({}, MOBILE, TABLET), device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isMobile()).toBe(true);
expect(inapp.isMobile).toBe(true);
}),
));
});
Expand All @@ -246,27 +207,7 @@ describe('InApp', () => {
_.forEach(DESKTOP, device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isMobile()).toBe(false);
}),
));
});
});

describe('isTablet', () => {
it('is tablet', () => {
_.forEach(TABLET, device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isTablet()).toBe(true);
}),
));
});

it('is not tablet', () => {
_.forEach(Object.assign({}, DESKTOP, MOBILE), device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isTablet()).toBe(false);
expect(inapp.isMobile).toBe(false);
}),
));
});
Expand All @@ -277,7 +218,7 @@ describe('InApp', () => {
_.forEach(DESKTOP, device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isDesktop()).toBe(true);
expect(inapp.isDesktop).toBe(true);
}),
));
});
Expand All @@ -286,7 +227,7 @@ describe('InApp', () => {
_.forEach(Object.assign({}, MOBILE, TABLET), device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isDesktop()).toBe(false);
expect(inapp.isDesktop).toBe(false);
}),
));
});
Expand All @@ -300,7 +241,7 @@ describe('InApp', () => {
_.forEach(Object.assign({}, MOBILE, TABLET), device => _.forEach(device, (useragents, name) =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isInApp()).toBe(['chrome', 'safari', 'ie', 'firefox'].indexOf(name.toLocaleLowerCase()) < 0);
expect(inapp.isInApp).toBe(['CHROME', 'SAFARI', 'FIREFOX', 'MIUI', 'PUFFIN'].indexOf(name) < 0);
}),
));
});
Expand All @@ -309,18 +250,9 @@ describe('InApp', () => {
_.forEach(DESKTOP, device => _.forEach(device, useragents =>
_.forEach(useragents, (useragent) => {
const inapp = new InApp(useragent);
expect(inapp.isInApp()).toBe(false);
expect(inapp.isInApp).toBe(false);
}),
));
});
});

describe('isApplePay', () => {
});

describe('isInstalled', () => {
});

describe('addMatch', () => {
});
});
Loading

0 comments on commit b82a67e

Please sign in to comment.