Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Unified Diff: js/tests/stateReflector.js

Issue 1204793003: Add tests for sk.object functions and sk.stateReflector. (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « js/tests/object.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: js/tests/stateReflector.js
diff --git a/js/tests/stateReflector.js b/js/tests/stateReflector.js
new file mode 100644
index 0000000000000000000000000000000000000000..782fcf795feda1506b6aafd17dd77eaeefa9f9cf
--- /dev/null
+++ b/js/tests/stateReflector.js
@@ -0,0 +1,88 @@
+describe('sk.stateReflector',
+ function() {
+ var clock;
+
+ afterEach(function () { if (clock) { clock.restore(); } });
+
+ function testStateReflector() {
+ clock = sinon.useFakeTimers();
+ var page = {
+ state: {
+ height: 2.0,
+ name: "Fritz",
+ alive: true,
+ labels: "foo bar baz",
+ }
+ };
+
+ var initHistoryLength = window.history.length;
+ var spy = sinon.spy();
+ var nextResolveCallback;
+ var pending = new Promise(function(resolve, reject) {
+ nextResolveCallback = resolve;
+ var callback = function() {
+ spy();
+ nextResolveCallback();
+ };
+ sk.stateReflector(page, callback);
+ // Fake the polymer-ready event.
+ window.dispatchEvent(new Event('polymer-ready'));
+ }).then(function() {
+ assert.equal(spy.callCount, 1);
+ page.state.height = 1.5;
+ page.state.name = "Lucy";
+ clock.tick(200); // Causes timers to be called.
+ assert.equal(window.history.length - initHistoryLength, 1);
+ page.state.alive = false;
+ page.state.labels = "foo bar";
+ clock.tick(200); // Causes timers to be called.
+ assert.equal(window.history.length - initHistoryLength, 2);
+ assert.deepEqual(page.state,
+ { height: 1.5,
+ name: "Lucy",
+ alive: false,
+ labels: "foo bar" });
+ // Trigger popstate due to history.back() and wait for callback.
+ return new Promise(function (resolve, reject) {
+ nextResolveCallback = resolve;
+ window.history.back();
+ });
+ }).then(function() {
+ assert.equal(spy.callCount, 2);
+ assert.deepEqual(page.state,
+ { height: 1.5,
+ name: "Lucy",
+ alive: true,
+ labels: "foo bar baz" });
+ // Trigger popstate due to history.back() and wait for callback.
+ return new Promise(function (resolve, reject) {
+ nextResolveCallback = resolve;
+ window.history.back();
+ });
+ }).then(function() {
+ assert.equal(spy.callCount, 3);
+ assert.deepEqual(page.state,
+ { height: 2.0,
+ name: "Fritz",
+ alive: true,
+ labels: "foo bar baz" });
+ // Trigger popstate due to history.forward() and wait for callback.
+ return new Promise(function (resolve, reject) {
+ nextResolveCallback = resolve;
+ window.history.forward();
+ });
+ }).then(function() {
+ assert.equal(spy.callCount, 4);
+ assert.deepEqual(page.state,
+ { height: 1.5,
+ name: "Lucy",
+ alive: true,
+ labels: "foo bar baz" });
+ });
+ return pending;
+ }
+
+ it("should link browser history with a JS object's state",
+ testStateReflector);
+ }
+);
« no previous file with comments | « js/tests/object.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698