Skip to content

Commit

Permalink
Merge pull request #106 from kaimallea/dont-ref-window
Browse files Browse the repository at this point in the history
fix: stop referencing window
  • Loading branch information
kaimallea authored Sep 11, 2019
2 parents 375f102 + 416a086 commit 6e31696
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 26 deletions.
10 changes: 9 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
module.exports = require('@spotify/web-scripts/config/jest.config.js');
module.exports = {
roots: ['<rootDir>/src/__tests__'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
testRegex: '^.+\\.ts$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
preset: 'jest-puppeteer',
};
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@
"prebuild": "rm -rf cjs esm dist",
"build": "yarn build:library && yarn build:browser",
"build:library": "web-scripts build",
"build:browser": "parcel build --target browser --experimental-scope-hoisting --out-file isMobile.min.js src/index.browser.ts",
"test": "web-scripts test",
"build:browser": "parcel build --target browser --experimental-scope-hoisting --global isMobile --out-file isMobile.min.js src/index.browser.ts",
"test": "jest",
"lint": "web-scripts lint",
"commit": "web-scripts commit",
"release": "web-scripts release"
},
"husky": {
"hooks": {
"pre-commit": "web-scripts precommit",
"pre-commit": "web-scripts precommit --jest-config ./jest.config.js",
"commit-msg": "web-scripts commitmsg"
}
},
"devDependencies": {
"@spotify/web-scripts": "^1.2.0",
"@types/puppeteer": "^1.19.1",
"husky": "^3.0.3",
"parcel-bundler": "^1.12.3"
"jest-puppeteer": "^4.3.0",
"parcel-bundler": "^1.12.3",
"puppeteer": "^1.19.0",
"ts-jest": "^24.0.2"
}
}
52 changes: 52 additions & 0 deletions src/__tests__/browser.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import puppeteer from 'puppeteer';
import { isMobileResult } from '..';

describe('E2E Tests', () => {
test('isMobile global variable is present', async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setUserAgent('okhttp/3.0.0');
await page.addScriptTag({ path: './dist/isMobile.min.js' });

const isMobile: isMobileResult = await page.evaluate(() => isMobile);

expect(isMobile).toMatchInlineSnapshot(`
Object {
"amazon": Object {
"device": false,
"phone": false,
"tablet": false,
},
"android": Object {
"device": true,
"phone": false,
"tablet": false,
},
"any": true,
"apple": Object {
"device": false,
"ipod": false,
"phone": false,
"tablet": false,
},
"other": Object {
"blackberry": false,
"blackberry10": false,
"chrome": false,
"device": false,
"firefox": false,
"opera": false,
},
"phone": false,
"tablet": false,
"windows": Object {
"device": false,
"phone": false,
"tablet": false,
},
}
`);

await browser.close();
});
});
10 changes: 2 additions & 8 deletions src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isMobile, { isMobileResult } from './';
import isMobile from './';

/**
* This file is used to generate the browser version of this library.
Expand All @@ -11,10 +11,4 @@ import isMobile, { isMobileResult } from './';
* immediately, assigning its result to the `isMobile` global variable.
*/

declare global {
interface Window {
isMobile: isMobileResult;
}
}

window.isMobile = isMobile();
exports = isMobile();
Loading

0 comments on commit 6e31696

Please sign in to comment.