Skip to content

Commit 6593e31

Browse files
committed
refactor project structure
1 parent 21fc547 commit 6593e31

File tree

8 files changed

+110
-47
lines changed

8 files changed

+110
-47
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ jobs:
1818
- run: npm install
1919
- run: npm run lint
2020
- run: npm test
21+
- run: npm run coveralls

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ build/Release
3535
# Dependency directories
3636
node_modules/
3737
jspm_packages/
38+
package-lock.json
3839

3940
# Typescript v1 declaration files
4041
typings/
@@ -56,4 +57,3 @@ typings/
5657

5758
# dotenv environment variables file
5859
.env
59-

karma.conf.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
1+
const resolve = require('rollup-plugin-node-resolve')
2+
const commonjs = require('rollup-plugin-commonjs')
3+
const coverage = require('rollup-plugin-coverage')
4+
const alias = require('rollup-plugin-alias')
5+
6+
const bundleType = process.env.BUNDLE_TYPE
7+
const bundlePath = bundleType ? `dist/${bundleType}.js` : 'src/index.js'
8+
19
module.exports = function (config) {
210
config.set({
3-
frameworks: ['mocha', 'chai'],
4-
files: ['*.test.js'],
5-
reporters: ['progress'],
11+
frameworks: ['mocha', 'chai', 'source-map-support'],
12+
files: ['tests/**/*.test.js'],
13+
reporters: ['progress', 'coverage'],
614
preprocessors: {
7-
'*.test.js': ['webpack']
15+
'tests/**/*.test.js': ['rollup']
816
},
9-
port: 9876, // karma web server port
17+
rollupPreprocessor: {
18+
plugins: [
19+
resolve(),
20+
commonjs({
21+
namedExports: {
22+
'node_modules/chai/index.js': ['expect']
23+
}
24+
}),
25+
coverage({
26+
include: ['src/**/*.js']
27+
})
28+
],
29+
format: 'iife',
30+
name: 'historyThrottler',
31+
sourcemap: 'inline'
32+
},
33+
coverageReporter: {
34+
dir: 'coverage',
35+
reporters: [
36+
{ type: 'lcov', subdir: '.' },
37+
{ type: 'text-summary' }
38+
]
39+
},
40+
port: 9876,
1041
colors: true,
1142
logLevel: config.LOG_INFO,
12-
browsers: ['ChromeHeadless'],
1343
autoWatch: false,
14-
// singleRun: false, // Karma captures browsers, runs the tests and exits
15-
concurrency: Infinity
44+
concurrency: Infinity,
45+
singleRun: true,
46+
browsers: ['ChromeHeadlessNoSandbox'],
47+
customLaunchers: {
48+
ChromeHeadlessNoSandbox: {
49+
base: 'ChromeHeadless',
50+
flags: ['--no-sandbox']
51+
}
52+
}
1653
})
1754
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "history-throttler",
33
"version": "1.0.0",
44
"description": "A simple fix for unwanted duplicate history entries on the frontend",
5-
"main": "throttleHistory.js",
5+
"main": "src/throttleHistory.js",
66
"scripts": {
7-
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
7+
"test": "karma start karma.conf.js",
88
"lint": "standard",
9-
"lint:fix": "standard --fix"
9+
"lint:fix": "standard --fix",
10+
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
1011
},
1112
"author": {
1213
"name": "Miklos Bertalan",
@@ -35,11 +36,18 @@
3536
"karma-chai": "^0.1.0",
3637
"karma-chrome-launcher": "^2.2.0",
3738
"karma-mocha": "^1.3.0",
38-
"karma-webpack": "^2.0.4",
39+
"karma-coverage": "^1.1.1",
40+
"karma-rollup-preprocessor": "^5.0.1",
41+
"karma-source-map-support": "^1.2.0",
42+
"rollup": "^0.49.0",
43+
"rollup-plugin-commonjs": "^8.2.0",
44+
"rollup-plugin-coverage": "^0.1.4",
45+
"rollup-plugin-node-resolve": "^3.0.0",
3946
"mocha": "^3.5.0",
47+
"nyc": "11.1.0",
4048
"pre-push": "^0.1.1",
4149
"standard": "^10.0.3",
42-
"webpack": "^3.5.4"
50+
"coveralls": "^2.13.1"
4351
},
4452
"standard": {
4553
"env": [

throttleHistory.js renamed to src/throttleHistory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const originalPushState = history.pushState
22
let statePushedInCurrentTick = false
33

44
export default function pushState () {
5-
if (!statePushedInCurrentTick && document.readyState === 'completed') {
5+
if (!statePushedInCurrentTick && document.readyState === 'complete') {
66
// allow pushState if it is the first one since the last paint and the document is loaded
77
originalPushState.apply(history, arguments)
88
flagOn()

tests/throttleHistory.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect } from 'chai'
2+
import pushState from '../src/throttleHistory'
3+
import { pageLoad, nextRender } from './utils'
4+
5+
describe('do not allow push state before the load event', () => {
6+
// 'it' blocks run after the page load, so this must remain here
7+
const startLength = history.length
8+
pushState(undefined, '', '')
9+
expect(history.length).to.equal(startLength)
10+
})
11+
12+
describe('throttle history', () => {
13+
before(async () => {
14+
await pageLoad()
15+
})
16+
17+
it('should allow the first pushState call after a repaint', () => {
18+
const startLength = history.length
19+
pushState(undefined, '', '')
20+
expect(history.length).to.equal(startLength + 1)
21+
})
22+
23+
it('should throttle later pushStates by replacing them by replaceState', () => {
24+
const startLength = history.length
25+
pushState(undefined, '', '')
26+
pushState(undefined, '', '')
27+
expect(history.length).to.equal(startLength)
28+
})
29+
30+
it('should allow a new pushState after a repaint', async () => {
31+
const startLength = history.length
32+
pushState(undefined, '', '')
33+
expect(history.length).to.equal(startLength)
34+
35+
await nextRender()
36+
pushState(undefined, '', '')
37+
expect(history.length).to.equal(startLength + 1)
38+
})
39+
})

tests/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function nextRender () {
2+
return new Promise(requestAnimationFrame)
3+
}
4+
5+
export function pageLoad () {
6+
if (document.readyState === 'complete') {
7+
return Promise.resolve()
8+
}
9+
return new Promise(resolve => window.addEventListener('load', resolve))
10+
}

throttleHistory.test.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)