test_workers_reducer.js (3219B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { 7 updateCanDebugWorkers, 8 updateWorkers, 9 } = require("resource://devtools/client/application/src/actions/workers.js"); 10 11 const { 12 START_WORKER, 13 UNREGISTER_WORKER, 14 } = require("resource://devtools/client/application/src/constants.js"); 15 16 const { 17 workersReducer, 18 WorkersState, 19 } = require("resource://devtools/client/application/src/reducers/workers-state.js"); 20 21 add_task(async function () { 22 info("Test workers reducer: UPDATE_CAN_DEBUG_WORKERS action"); 23 24 function testUpdateCanDebugWorkers(flagValue) { 25 const state = WorkersState(); 26 const action = updateCanDebugWorkers(flagValue); 27 const newState = workersReducer(state, action); 28 equal( 29 newState.canDebugWorkers, 30 flagValue, 31 "canDebugWorkers contains the expected value" 32 ); 33 } 34 35 testUpdateCanDebugWorkers(false); 36 testUpdateCanDebugWorkers(true); 37 }); 38 39 add_task(async function () { 40 info("Test workers reducer: UPDATE_WORKERS action"); 41 const state = WorkersState(); 42 43 const rawData = [ 44 { 45 registration: { 46 scope: "lorem-ipsum", 47 lastUpdateTime: 42, 48 id: "r1", 49 }, 50 workers: [ 51 { 52 id: "w1", 53 state: Ci.nsIServiceWorkerInfo.STATE_ACTIVATED, 54 url: "https://example.com/w1.js", 55 workerDescriptorFront: { foo: "bar" }, 56 stateText: "activated", 57 }, 58 { 59 id: "w2", 60 state: Ci.nsIServiceWorkerInfo.STATE_INSTALLED, 61 url: "https://example.com/w2.js", 62 workerDescriptorFront: undefined, 63 stateText: "installed", 64 }, 65 ], 66 }, 67 ]; 68 69 const expectedData = [ 70 { 71 id: "r1", 72 lastUpdateTime: 42, 73 registrationFront: rawData[0].registration, 74 scope: "lorem-ipsum", 75 workers: [ 76 { 77 id: "w1", 78 url: "https://example.com/w1.js", 79 workerDescriptorFront: rawData[0].workers[0].workerDescriptorFront, 80 registrationFront: rawData[0].registration, 81 state: Ci.nsIServiceWorkerInfo.STATE_ACTIVATED, 82 stateText: "activated", 83 }, 84 { 85 id: "w2", 86 url: "https://example.com/w2.js", 87 workerDescriptorFront: undefined, 88 registrationFront: rawData[0].registration, 89 state: Ci.nsIServiceWorkerInfo.STATE_INSTALLED, 90 stateText: "installed", 91 }, 92 ], 93 }, 94 ]; 95 96 const action = updateWorkers(rawData); 97 const newState = workersReducer(state, action); 98 deepEqual(newState.list, expectedData, "workers contains the expected list"); 99 }); 100 101 add_task(async function () { 102 info("Test workers reducer: START_WORKER action"); 103 const state = WorkersState(); 104 const action = { type: START_WORKER }; 105 const newState = workersReducer(state, action); 106 deepEqual(state, newState, "workers state stays the same"); 107 }); 108 109 add_task(async function () { 110 info("Test workers reducer: UNREGISTER_WORKER action"); 111 const state = WorkersState(); 112 const action = { type: UNREGISTER_WORKER }; 113 const newState = workersReducer(state, action); 114 deepEqual(state, newState, "workers state stays the same"); 115 });