From ec305e667da5219efa8bc961c2f6ee5df2656cca Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Sun, 6 Nov 2022 05:46:23 +0000 Subject: [PATCH] switch unit test framework to jest, remove framework unittest, add helper module to create unit testing environment --- .gitignore | 3 ++ testenv/package.json | 10 ++++ testenv/testenv.js | 28 ++++++++++ testenv/testenv.test.js | 86 +++++++++++++++++++++++++++++ unittest/unittest.js | 59 -------------------- unittest/unittest.test.js | 110 -------------------------------------- 6 files changed, 127 insertions(+), 169 deletions(-) create mode 100644 .gitignore create mode 100644 testenv/package.json create mode 100644 testenv/testenv.js create mode 100644 testenv/testenv.test.js delete mode 100644 unittest/unittest.js delete mode 100644 unittest/unittest.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f2c69d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/.devcontainer/* +**/node_modules/* +**/package-lock.json \ No newline at end of file diff --git a/testenv/package.json b/testenv/package.json new file mode 100644 index 0000000..0aedc61 --- /dev/null +++ b/testenv/package.json @@ -0,0 +1,10 @@ +{ + "name": "testenv", + "version": "1.0.0", + "scripts": { + "test": "jest" + }, + "dependencies": { + "jest": "24" + } +} diff --git a/testenv/testenv.js b/testenv/testenv.js new file mode 100644 index 0000000..e62cfc3 --- /dev/null +++ b/testenv/testenv.js @@ -0,0 +1,28 @@ +module.exports = {environment}; + +function environment () { + const localStorageMock = (function () { + let store = {}; + return { + getItem(key) { + return store[key]; + }, + setItem(key, value) { + store[key] = value; + }, + clear() { + store = {}; + }, + removeItem(key) { + delete store[key]; + }, + getAll() { + return store; + }, + }; + })(); + + let window = {}; + Object.defineProperty(window, "localStorage", { value: localStorageMock }); + return window; +} \ No newline at end of file diff --git a/testenv/testenv.test.js b/testenv/testenv.test.js new file mode 100644 index 0000000..27c8981 --- /dev/null +++ b/testenv/testenv.test.js @@ -0,0 +1,86 @@ +const {environment} = require("./testenv.js"); + +function saveToLocal (k, v) { + window.localStorage.setItem(k, v); +} + +function getFromLocal (k) { + return window.localStorage.getItem(k); +} + +function removeFromLocal (k) { + window.localStorage.removeItem(k); +} + +function clearLocal () { + window.localStorage.clear(); +} + +describe("test localStorage mock", () => { + test("test save and fetch", () => { + let window = environment(); + saveToLocal("testkey1", "testvalue1"); + saveToLocal("testkey2", "testvalue2"); + saveToLocal("testkey3", "testvalue3"); + saveToLocal("testkey4", "testvalue4"); + + expect(getFromLocal("testkey1")).toBe("testvalue1"); + expect(getFromLocal("testkey2")).toBe("testvalue2"); + expect(getFromLocal("testkey3")).toBe("testvalue3"); + expect(getFromLocal("testkey4")).toBe("testvalue4"); + + saveToLocal("testkey6", "testvalue5"); + expect(getFromLocal("testkey6")).toBe("testvalue5"); + }); + + test("test delete and fetch", () => { + let window = environment(); + saveToLocal("testkey1", "testvalue1"); + saveToLocal("testkey2", "testvalue2"); + saveToLocal("testkey3", "testvalue3"); + saveToLocal("testkey4", "testvalue4"); + + removeFromLocal("testkey3"); + + expect(getFromLocal("testkey1")).toBe("testvalue1"); + expect(getFromLocal("testkey2")).toBe("testvalue2"); + expect(getFromLocal("testkey3")).toBe(null); + expect(getFromLocal("testkey4")).toBe("testvalue4"); + + removeFromLocal("testkey1"); + + expect(getFromLocal("testkey1")).toBe(null); + expect(getFromLocal("testkey2")).toBe("testvalue2"); + expect(getFromLocal("testkey3")).toBe(null); + expect(getFromLocal("testkey4")).toBe("testvalue4"); + + removeFromLocal("testkey4"); + + expect(getFromLocal("testkey1")).toBe(null); + expect(getFromLocal("testkey2")).toBe("testvalue2"); + expect(getFromLocal("testkey3")).toBe(null); + expect(getFromLocal("testkey4")).toBe(null); + + removeFromLocal("testkey2"); + + expect(getFromLocal("testkey1")).toBe(null); + expect(getFromLocal("testkey2")).toBe(null); + expect(getFromLocal("testkey3")).toBe(null); + expect(getFromLocal("testkey4")).toBe(null); + }); + + test("test delete and fetch", () => { + let window = environment(); + saveToLocal("testkey1", "testvalue1"); + saveToLocal("testkey2", "testvalue2"); + saveToLocal("testkey3", "testvalue3"); + saveToLocal("testkey4", "testvalue4"); + + clearLocal(); + + expect(getFromLocal("testkey1")).toBe(null); + expect(getFromLocal("testkey2")).toBe(null); + expect(getFromLocal("testkey3")).toBe(null); + expect(getFromLocal("testkey4")).toBe(null); + }); +}); \ No newline at end of file diff --git a/unittest/unittest.js b/unittest/unittest.js deleted file mode 100644 index bf894fd..0000000 --- a/unittest/unittest.js +++ /dev/null @@ -1,59 +0,0 @@ -module.exports = {test, item, assert}; - -var itemsFailed; - -/* - * Calls multiple item functions. - * - * param name string name of the test - * param items func callable function that contains item calls - * - * return bool whether the test passed or failed -*/ -function test(name, items) { - console.log(name); - items(); -} - -/* - * Contain multiple assertions to test specific behavior - * - * param name string name of the specific item - * param items func multple assertion calls - * - * return bool whether the item passed or failed -*/ -function item(name, method) { - try{ - method(); - console.log('\t\x1b[32m%s\x1b[0m', '\u2714 ' + name); - itemsFailed += 0; - return true; - } - catch (error) { - console.log('\t\x1b[31m%s\x1b[0m', '\u2718 ' + name); - itemsFailed += 1; - return false; - } -} - -/* - * Given two inputs, comapres if they equivalent. - * - * param input any output of some function to be tested. - * param expected any expected output of the function - * - * return bool whether input = expected -*/ -function assert(input, expected) { - let result; - if (typeof(input) === 'object') { - result = JSON.stringify(input) === JSON.stringify(expected); - } - else { - result = input === expected; - } - if(!result){ - throw new Error(); - } -} \ No newline at end of file diff --git a/unittest/unittest.test.js b/unittest/unittest.test.js deleted file mode 100644 index 16c0d4e..0000000 --- a/unittest/unittest.test.js +++ /dev/null @@ -1,110 +0,0 @@ -const unittest = require("./unittest.js"); - -const localStorageMock = (function () { - let store = {}; - return { - getItem(key) { - return store[key]; - }, - setItem(key, value) { - store[key] = value; - }, - clear() { - store = {}; - }, - removeItem(key) { - delete store[key]; - }, - getAll() { - return store; - }, - }; -})(); -let window = {}; -Object.defineProperty(window, "localStorage", { value: localStorageMock }); - -function assertion_test () { - try { unittest.assert(1, 1); } - catch (error) { console.log("Failed assert(1,1)") } - - try { unittest.assert(65536, 65536); } - catch (error) { console.log("Failed assert(655366,65536)") } - - try { unittest.assert(-50, 50); console.log("Failed assert(-50,50)") } - catch (error) { } - - try { unittest.assert(0, 1); console.log("Failed assert(0,1)") } - catch (error) { } - - try { unittest.assert(true, true); } - catch (error) { console.log("Failed assert(true,true)") } - - try { unittest.assert(false, false); } - catch (error) { console.log("Failed assert(false,false)") } - - try { unittest.assert(true, false); console.log("Failed assert(true,false)") } - catch (error) { } - - try { unittest.assert(false, true); console.log("Failed assert(false,true)") } - catch (error) { } - - try { unittest.assert("Test", "Test"); } - catch (error) { console.log("Failed assert('Test','Test')") } - - try { unittest.assert("Test", "Yay"); console.log("Failed assert('Test','Yay')") } - catch (error) { } - - try { unittest.assert({one: 1, two: 2}, {one: 1, two: 2}); } - catch (error) { console.log("Failed assert({one: 1, two: 2},{one: 1, two: 2})") } - - try { unittest.assert({one: 1, two: 2}, {two: 2, three: 3}); console.log("Failed assert({one: 1, two: 2}, {two: 2, three: 3})") } - catch (error) { } -} -assertion_test(); - -function item_test () { - unittest.item("passing item", () => { - unittest.assert(1, 1); - }); - unittest.item("failing item", () => { - unittest.assert(1, 2); - }); -} -item_test(); - -function test_test () { - unittest.test("example test", () => { - unittest.item("assert 1 = 1", () => { - unittest.assert(1, 1); - }); - unittest.item("assert 1 = 2", () => { - unittest.assert(1, 2); - }) - }); -} -test_test(); - -function environment_test () { - function testfunc1 () { - window.localStorage.clear(); - window.localStorage.setItem("testkey", "testvalue"); - return window.localStorage.getItem("testkey"); - } - - function testfunc2 (){ - window.localStorage.removeItem("testkey"); - } - - unittest.test("test add item", () => { - unittest.item("testfunc1", () => { - unittest.assert(testfunc1(), "testvalue"); - }); - }); - - unittest.test("test delete item", () => { - unittest.item("testfunc2", () => { - unittest.assert(testfunc2(), undefined); - }); - }); -} -environment_test(); \ No newline at end of file