File tree 7 files changed +154
-12
lines changed
7 files changed +154
-12
lines changed Original file line number Diff line number Diff line change 6
6
"scripts" : {
7
7
"build" : " rollup -c rollup.config.js" ,
8
8
"watch" : " npm run build -- -w" ,
9
- "test" : " mocha src/**/*.spec.js --require babel-register --require babel-polyfill" ,
9
+ "test" : " mocha src/**/*.spec.js --require babel-register --require babel-polyfill --require testSetup.js " ,
10
10
"test-watch" : " npm run test -- -w"
11
11
},
12
12
"repository" : {
25
25
"babel-register" : " ^6.26.0" ,
26
26
"chai" : " ^4.2.0" ,
27
27
"mocha" : " ^5.2.0" ,
28
+ "node-fetch" : " ^2.3.0" ,
28
29
"rollup" : " ^1.0.0" ,
29
- "rollup-copy-plugin" : " ^0.1.0"
30
+ "rollup-copy-plugin" : " ^0.1.0" ,
31
+ "sinon" : " ^7.2.2" ,
32
+ "sinon-chai" : " ^3.3.0"
30
33
},
31
34
"dependencies" : {}
32
35
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ const policy = {
17
17
describe ( 'applyRules' , ( ) => {
18
18
it ( 'cancels blocked url' , ( ) => {
19
19
const blockingResponse = applyRules ( policy ) ( { url : 'http://www.notarealwebsite.com' } )
20
- const expected = "src/blockedPage/ blocked.html?union=pineapple&msg=not%20okay"
20
+ const expected = "blocked.html?union=pineapple&msg=not%20okay"
21
21
expect ( blockingResponse ) . to . deep . equal ( { redirectUrl : expected } )
22
22
} )
23
23
Original file line number Diff line number Diff line change @@ -9,14 +9,9 @@ const fetchPolicyFile = (policyFileUrl) => {
9
9
. then ( resp => resp . json ( ) )
10
10
}
11
11
12
- const loadPolicy = ( form ) => ( ev ) => {
13
- ev . preventDefault ( ) ;
12
+ export default ( form ) => {
14
13
const policyFileUrl = new FormData ( form ) . get ( 'policyFile' )
15
- fetchPolicyFile ( policyFileUrl ) . then ( p => storePolicyForUser ( p , policyFileUrl ) ) ;
14
+ return fetchPolicyFile ( policyFileUrl )
15
+ . then ( p => storePolicyForUser ( p , policyFileUrl ) ) ;
16
16
}
17
17
18
- document . addEventListener ( 'DOMContentLoaded' , ( ) => {
19
- const policyFileForm = document . getElementById ( 'policyFileForm' )
20
- policyFileForm . addEventListener ( 'submit' , loadPolicy ( policyFileForm ) )
21
- } )
22
-
Original file line number Diff line number Diff line change
1
+ import loadPolicy from './loadPolicy.js'
2
+ import { stub } from 'sinon'
3
+ import { expect } from 'chai'
4
+
5
+ describe ( 'loadPolicy' , ( ) => {
6
+ beforeEach ( ( ) => {
7
+ global . fetch = stub ( ) . resolves ( { json : ( ) => ( { policy : 'somepolicy' } ) } )
8
+ global . chrome = { storage : { sync : { set : stub ( ) } } }
9
+ global . FormData = class FormData { get ( ) { return 'policyFileURL' } }
10
+ } )
11
+
12
+ afterEach ( ( ) => {
13
+ delete global . fetch
14
+ delete global . chrome
15
+ delete global . FormData
16
+ } )
17
+
18
+ it ( 'fetches policy from url' , ( ) => {
19
+ return loadPolicy ( ) . then ( ( ) => {
20
+ expect ( global . fetch ) . to . have . been . calledWith ( 'policyFileURL' )
21
+ } )
22
+ } )
23
+
24
+ it ( 'sets policy in storage' , ( ) => {
25
+ return loadPolicy ( ) . then ( ( ) => {
26
+ const args = global . chrome . storage . sync . set . firstCall . args [ 0 ]
27
+ expect ( args . policy ) . to . deep . equal ( { policy : 'somepolicy' } )
28
+ expect ( args . policyFileUrl ) . to . deep . equal ( 'policyFileURL' )
29
+ } )
30
+ } )
31
+ } )
Original file line number Diff line number Diff line change 1
- import './loadPolicy'
1
+ import loadPolicy from './loadPolicy'
2
+
3
+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
4
+ const policyFileForm = document . getElementById ( 'policyFileForm' )
5
+ policyFileForm . addEventListener ( 'submit' , ( ev ) => {
6
+ ev . preventDefault ( ) ;
7
+ loadPolicy ( policyFileForm )
8
+ } )
9
+ } )
Original file line number Diff line number Diff line change
1
+ const chai = require ( "chai" ) ;
2
+ const sinonChai = require ( "sinon-chai" ) ;
3
+
4
+ chai . use ( sinonChai ) ;
5
+
You can’t perform that action at this time.
0 commit comments