mirror of
https://github.com/Feodor2/Mypal68.git
synced 2025-06-18 14:55:44 -04:00
169 lines
4.4 KiB
HTML
169 lines
4.4 KiB
HTML
<!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>
|