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.
This commit is contained in:
Pierre Ossman 2023-04-06 17:49:44 +02:00
parent 05b6d2ad67
commit cd1a63b737
1 changed files with 17 additions and 8 deletions

View File

@ -8,39 +8,48 @@ describe('WebUtil', function () {
"use strict"; "use strict";
describe('config variables', function () { 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 () { it('should parse query string variables', function () {
// history.pushState() will not cause the browser to attempt loading // history.pushState() will not cause the browser to attempt loading
// the URL, this is exactly what we want here for the tests. // 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"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval");
}); });
it('should return default value when no query match', function () { 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"); expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def");
}); });
it('should handle no query match and no default value', function () { 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); expect(WebUtil.getConfigVar("other")).to.be.equal(null);
}); });
it('should parse fragment variables', function () { it('should parse fragment variables', function () {
history.pushState({}, '', "test#myvar=myval"); history.replaceState({}, '', "test#myvar=myval");
expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval");
}); });
it('should return default value when no fragment match', function () { 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"); expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def");
}); });
it('should handle no fragment match and no default value', function () { 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); expect(WebUtil.getConfigVar("other")).to.be.equal(null);
}); });
it('should handle both query and fragment', function () { 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("myquery")).to.be.equal("1");
expect(WebUtil.getConfigVar("myhash")).to.be.equal("2"); expect(WebUtil.getConfigVar("myhash")).to.be.equal("2");
}); });
it('should prioritize fragment if both provide same var', function () { 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"); expect(WebUtil.getConfigVar("myvar")).to.be.equal("2");
}); });
}); });