Replace local storage variables with class in tests

This commit is contained in:
felixlinker
2018-10-21 19:59:21 +01:00
parent 3315570edf
commit 4613e7ca7a

View File

@@ -6,23 +6,53 @@ import TU from 'react-testutils-additions';
let origAddEventListener = window.addEventListener; let origAddEventListener = window.addEventListener;
let storageListener; let storageListener;
let ls = {};
// Implment mock localStorage /**
let localStorage = { * Mock local storage
getItem: (key) => { */
return ls[key]; class LocalStorage {
}, /**
setItem: (key, value) => { * Sets up storage variable
ls[key] = value; */
}, constructor() {
removeItem: (key) => { this.ls = {};
delete ls[key];
},
clear: () => {
ls = {};
} }
};
/**
* Retrieves a value from local storage
* @param {String} key Storage key
* @return {*} Storage value
*/
getItem(key) {
return this.ls[key];
}
/**
* Writes a value to local storage
* @param {String} key Storage key
* @param {*} value Storage value
*/
setItem(key, value) {
this.ls[key] = value;
}
/**
* Clears a value from local storage
* @param {String} key Storage key
*/
removeItem(key) {
delete this.ls[key];
}
/**
* Clears local storage
*/
clear() {
this.ls = {};
}
}
const LOCAL_STORAGE = new LocalStorage();
window.addEventListener = function(eventName, listener) { window.addEventListener = function(eventName, listener) {
if(eventName == 'storage') { if(eventName == 'storage') {
@@ -38,7 +68,8 @@ describe('Persist', function() {
describe('Multi tab/window', function() { describe('Multi tab/window', function() {
it("syncs builds", function() { it("syncs builds", function() {
window.localStorage = localStorage; window.localStorage = localStorage;
ls = {}; localStorage.clear();
let p = new Persist(); let p = new Persist();
let newBuilds = { let newBuilds = {
anaconda: { test: '1234' } anaconda: { test: '1234' }
@@ -52,7 +83,8 @@ describe('Persist', function() {
describe('General and Settings', function() { describe('General and Settings', function() {
it("has defaults", function() { it("has defaults", function() {
window.localStorage = localStorage; window.localStorage = localStorage;
ls = {}; localStorage.clear();
let p = new Persist(); let p = new Persist();
expect(p.getLangCode()).toBe('en'); expect(p.getLangCode()).toBe('en');
expect(p.showTooltips()).toBe(true); expect(p.showTooltips()).toBe(true);
@@ -63,14 +95,14 @@ describe('Persist', function() {
}); });
it("loads from localStorage correctly", function() { it("loads from localStorage correctly", function() {
window.localStorage = localStorage; window.localStorage = LOCAL_STORAGE;
let savedData = require('./fixtures/valid-backup'); let savedData = require('./fixtures/valid-backup');
ls = {}; LOCAL_STORAGE.clear();
ls.builds = JSON.stringify(savedData.builds); LOCAL_STORAGE.setItem('builds', JSON.stringify(savedData.builds));
ls.NG_TRANSLATE_LANG_KEY = 'de'; LOCAL_STORAGE.setItem('NG_TRANSLATE_LANG_KEY', 'de');
ls.insurance = 'Standard'; LOCAL_STORAGE.setItem('insurance', 'Standard');
ls.shipDiscount = 0.25; LOCAL_STORAGE.setItem('shipDiscount', 0.25);
ls.moduleDiscount = 0.15; LOCAL_STORAGE.setItem('moduleDiscount', 0.15);
let p = new Persist(); let p = new Persist();
expect(p.getInsurance()).toBe('standard'); expect(p.getInsurance()).toBe('standard');
@@ -84,13 +116,13 @@ describe('Persist', function() {
}); });
it("uses defaults from a corrupted localStorage", function() { it("uses defaults from a corrupted localStorage", function() {
window.localStorage = localStorage; window.localStorage = LOCAL_STORAGE;
ls = {}; LOCAL_STORAGE.clear();
ls.builds = "not valid json"; LOCAL_STORAGE.setItem('builds', 'not valid json');
ls.comparisons = "1, 3, 4"; LOCAL_STORAGE.setItem('comparisons', '1, 3, 4');
ls.insurance = 'this insurance does not exist'; LOCAL_STORAGE.setItem('insurance', 'this insurance does not exist');
ls.shipDiscount = 'this is not a number'; LOCAL_STORAGE.setItem('shipDiscount', 'this is not a number');
ls.moduleDiscount = 10; // Way to big LOCAL_STORAGE.setItem('moduleDiscount', 10); // Way to big
let p = new Persist(); let p = new Persist();
expect(p.getLangCode()).toBe('en'); expect(p.getLangCode()).toBe('en');
@@ -120,13 +152,13 @@ describe('Persist', function() {
}); });
it("generates the backup", function() { it("generates the backup", function() {
window.localStorage = localStorage; window.localStorage = LOCAL_STORAGE;
let savedData = require('./fixtures/valid-backup'); let savedData = require('./fixtures/valid-backup');
ls = {}; LOCAL_STORAGE.clear();
ls.builds = JSON.stringify(savedData.builds); LOCAL_STORAGE.setItem('builds', JSON.stringify(savedData.builds));
ls.insurance = 'Beta'; LOCAL_STORAGE.setItem('insurance', 'Beta');
ls.shipDiscount = 0.25; LOCAL_STORAGE.setItem('shipDiscount', 0.25);
ls.moduleDiscount = 0.15; LOCAL_STORAGE.setItem('moduleDiscount', 0.15);
let p = new Persist(); let p = new Persist();
let backup = p.getAll(); let backup = p.getAll();