1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
<!DOCTYPE HTML>
<html>
<!--
Test the PaymentsStore
-->
<head>
<meta charset="utf-8">
<title>Test the PaymentsStore</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="sinon-7.2.7.js"></script>
<script src="payments_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script type="module">
/** Test the PaymentsStore **/
/* global sinon */
import PaymentsStore from "../../res/PaymentsStore.js";
function assert_throws(block, expectedError, message) {
let actual;
try {
block();
} catch (e) {
actual = e;
}
ok(actual, "Expecting exception: " + message);
ok(actual instanceof expectedError,
`Check error type is ${expectedError.prototype.name}: ${message}`);
}
add_task(async function test_defaultState() {
ok(!!PaymentsStore, "Check PaymentsStore import");
let ps = new PaymentsStore({
foo: "bar",
});
let state = ps.getState();
ok(!!state, "Check state is truthy");
is(state.foo, "bar", "Check .foo");
assert_throws(() => state.foo = "new", TypeError, "Assigning to existing prop. should throw");
assert_throws(() => state.other = "something", TypeError, "Adding a new prop. should throw");
assert_throws(() => delete state.foo, TypeError, "Deleting a prop. should throw");
});
add_task(async function test_setState() {
let ps = new PaymentsStore({});
ps.setState({
one: "one",
});
let state = ps.getState();
is(Object.keys(state).length, 1, "Should only have 1 prop. set");
is(state.one, "one", "Check .one");
ps.setState({
two: 2,
});
state = ps.getState();
is(Object.keys(state).length, 2, "Should have 2 props. set");
is(state.one, "one", "Check .one");
is(state.two, 2, "Check .two");
ps.setState({
one: "a",
two: "b",
});
state = ps.getState();
is(state.one, "a", "Check .one");
is(state.two, "b", "Check .two");
info("check consecutive setState for the same prop");
ps.setState({
one: "c",
});
ps.setState({
one: "d",
});
state = ps.getState();
is(Object.keys(state).length, 2, "Should have 2 props. set");
is(state.one, "d", "Check .one");
is(state.two, "b", "Check .two");
});
add_task(async function test_subscribe_unsubscribe() {
let ps = new PaymentsStore({});
let subscriber = {
stateChangePromise: null,
_stateChangeResolver: null,
reset() {
this.stateChangePromise = new Promise(resolve => {
this._stateChangeResolver = resolve;
});
},
stateChangeCallback(state) {
this._stateChangeResolver(state);
this.stateChangePromise = new Promise(resolve => {
this._stateChangeResolver = resolve;
});
},
};
sinon.spy(subscriber, "stateChangeCallback");
subscriber.reset();
ps.subscribe(subscriber);
info("subscribe the same listener twice to ensure it still doesn't call the callback");
ps.subscribe(subscriber);
ok(subscriber.stateChangeCallback.notCalled,
"Check not called synchronously when subscribing");
let changePromise = subscriber.stateChangePromise;
ps.setState({
a: 1,
});
ok(subscriber.stateChangeCallback.notCalled,
"Check not called synchronously for changes");
let state = await changePromise;
is(state, subscriber.stateChangeCallback.getCall(0).args[0],
"Check resolved state is last state");
is(JSON.stringify(state), `{"a":1}`, "Check callback state");
info("Testing consecutive setState");
subscriber.reset();
subscriber.stateChangeCallback.reset();
changePromise = subscriber.stateChangePromise;
ps.setState({
a: 2,
});
ps.setState({
a: 3,
});
ok(subscriber.stateChangeCallback.notCalled,
"Check not called synchronously for changes");
state = await changePromise;
is(state, subscriber.stateChangeCallback.getCall(0).args[0],
"Check resolved state is last state");
is(JSON.stringify(subscriber.stateChangeCallback.getCall(0).args[0]), `{"a":3}`,
"Check callback state matches second setState");
info("test unsubscribe");
subscriber.stateChangeCallback = function unexpectedChange() {
ok(false, "stateChangeCallback shouldn't be called after unsubscribing");
};
ps.unsubscribe(subscriber);
ps.setState({
a: 4,
});
await Promise.resolve("giving a chance for the callback to be called");
});
</script>
</body>
</html>
|