From 9bcc9b3a50d0b94d33d82a766474b802e482ccdf Mon Sep 17 00:00:00 2001 From: Zach <12666484+ZachMayry@users.noreply.github.com> Date: Tue, 15 Dec 2020 11:07:38 -0600 Subject: [PATCH] fix: empty string no longer coalesces into null (#121) * fix: empty string no longer coalesces into null * fix: empty string no longer coalesces into null Co-authored-by: Zachary Mayry Co-authored-by: Bryan Clark --- __tests__/localstorage.test.js | 20 ++++++++++++++++++++ src/localstorage.js | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 __tests__/localstorage.test.js diff --git a/__tests__/localstorage.test.js b/__tests__/localstorage.test.js new file mode 100644 index 0000000..c17916d --- /dev/null +++ b/__tests__/localstorage.test.js @@ -0,0 +1,20 @@ +import { LocalStorage } from "../src/localstorage"; + +describe("localstorage", () => { + let localStorage; + + beforeEach(() => { + localStorage = new LocalStorage(jest); + }); + + describe("getItem", () => { + it("should return null if the item is undefined", () => { + expect(localStorage.getItem("item")).toBeNull(); + }); + + it("should return '' instead of null", () => { + localStorage.setItem("item", ""); + expect(localStorage.getItem("item")).toBe(""); + }); + }); +}); diff --git a/src/localstorage.js b/src/localstorage.js index b1dd3ab..95a5a1b 100644 --- a/src/localstorage.js +++ b/src/localstorage.js @@ -2,7 +2,7 @@ export class LocalStorage { constructor(jest) { Object.defineProperty(this, 'getItem', { enumerable: false, - value: jest.fn(key => this[key] || null), + value: jest.fn(key => this[key] !== undefined ? this[key] : null), }); Object.defineProperty(this, 'setItem', { enumerable: false,