Skip to content

Commit

Permalink
Fix local storage bug and change key (#56) - #minor
Browse files Browse the repository at this point in the history
- #52 - enable fallback for when local storage is not available
- #50 - change local storage key value
- #50 - bump packages.json version number
  • Loading branch information
jnahumphreys authored Jan 24, 2024
1 parent 6967835 commit e2caf45
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
12 changes: 12 additions & 0 deletions cypress/e2e/app.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ describe("React Counter App", () => {
it("prevents a user decrementing the count below zero", () => {
cy.get("@decrementValue").should("be.disabled");
});

it("supports clients with local storage disabled", () => {
cy.window().then((win) => {
cy.stub(win.localStorage, "setItem").throws();
cy.stub(win.localStorage, "getItem").throws();

cy.get("@incrementValue").click();
cy.get("@countValue").should("contain", "1");
cy.reload();
cy.get("@countValue").should("contain", "0");
});
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-counter-app",
"version": "1.15.0",
"version": "1.16.0",
"description": "React counter app",
"engines": {
"node": ">=18.15.0 <19.0.0"
Expand Down
16 changes: 12 additions & 4 deletions src/app/counter-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@ function counterReducer(
}
}

const LOCAL_STORAGE_KEY = "binaryJimCounterAppValue" as const;
const LOCAL_STORAGE_KEY = "jnahumphreys_counter_app_value" as const;

function setLocalStorageCountValue(value: number) {
localStorage.setItem(LOCAL_STORAGE_KEY, value.toString());
try {
localStorage.setItem(LOCAL_STORAGE_KEY, value.toString());
} catch (error) {
return false;
}
}

function getLocalStorageCountValue() {
const localStorageValue = localStorage.getItem(LOCAL_STORAGE_KEY);
try {
const localStorageValue = localStorage.getItem(LOCAL_STORAGE_KEY);

return localStorageValue ? Number(localStorageValue) : false;
return localStorageValue ? Number(localStorageValue) : false;
} catch (error) {
return false;
}
}

function initCounterReducerState(initialValue: number): CounterReducerState {
Expand Down

0 comments on commit e2caf45

Please sign in to comment.