From cd1a63b737b1ffa0fb5a343de7381bedf562c5e4 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 6 Apr 2023 17:49:44 +0200 Subject: Restore history state after tests We don't want to mess up anything permanent in each test or the tests might start affecting each other. --- tests/test.webutil.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/test.webutil.js b/tests/test.webutil.js index 76aa763..6f460a3 100644 --- a/tests/test.webutil.js +++ b/tests/test.webutil.js @@ -8,39 +8,48 @@ describe('WebUtil', function () { "use strict"; describe('config variables', function () { + let origState, origHref; + beforeEach(function () { + origState = history.state; + origHref = location.href; + }); + afterEach(function () { + history.replaceState(origState, '', origHref); + }); + it('should parse query string variables', function () { // history.pushState() will not cause the browser to attempt loading // the URL, this is exactly what we want here for the tests. - history.pushState({}, '', "test?myvar=myval"); + history.replaceState({}, '', "test?myvar=myval"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval"); }); it('should return default value when no query match', function () { - history.pushState({}, '', "test?myvar=myval"); + history.replaceState({}, '', "test?myvar=myval"); expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def"); }); it('should handle no query match and no default value', function () { - history.pushState({}, '', "test?myvar=myval"); + history.replaceState({}, '', "test?myvar=myval"); expect(WebUtil.getConfigVar("other")).to.be.equal(null); }); it('should parse fragment variables', function () { - history.pushState({}, '', "test#myvar=myval"); + history.replaceState({}, '', "test#myvar=myval"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval"); }); it('should return default value when no fragment match', function () { - history.pushState({}, '', "test#myvar=myval"); + history.replaceState({}, '', "test#myvar=myval"); expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def"); }); it('should handle no fragment match and no default value', function () { - history.pushState({}, '', "test#myvar=myval"); + history.replaceState({}, '', "test#myvar=myval"); expect(WebUtil.getConfigVar("other")).to.be.equal(null); }); it('should handle both query and fragment', function () { - history.pushState({}, '', "test?myquery=1#myhash=2"); + history.replaceState({}, '', "test?myquery=1#myhash=2"); expect(WebUtil.getConfigVar("myquery")).to.be.equal("1"); expect(WebUtil.getConfigVar("myhash")).to.be.equal("2"); }); it('should prioritize fragment if both provide same var', function () { - history.pushState({}, '', "test?myvar=1#myvar=2"); + history.replaceState({}, '', "test?myvar=1#myvar=2"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("2"); }); }); -- cgit v1.2.1