-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: initialize localforage instances in respective repos
Signed-off-by: rare-magma <[email protected]>
- Loading branch information
1 parent
f1abc8a
commit 36fdb9c
Showing
7 changed files
with
225 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
type PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
import type { | ||
CurrencyInputProps, | ||
IntlConfig, | ||
} from "react-currency-input-field/dist/components/CurrencyInputProps"; | ||
import { localForageOptionsRepository } from "../infrastructure/localForageOptionsRepository"; | ||
|
||
interface ConfigContextInterface { | ||
intlConfig: IntlConfig | undefined; | ||
setIntlConfig: (value: IntlConfig) => void; | ||
currency: string; | ||
setCurrency: (value: string) => void; | ||
} | ||
|
||
const optionsRepository = new localForageOptionsRepository(); | ||
|
||
const ConfigContext = createContext<ConfigContextInterface>({ | ||
intlConfig: { | ||
locale: optionsRepository.getUserLang(), | ||
currency: optionsRepository.getDefaultCurrencyCode(), | ||
}, | ||
setIntlConfig: (value: IntlConfig) => value, | ||
currency: optionsRepository.getDefaultCurrencyCode(), | ||
setCurrency: (value: string) => value, | ||
}); | ||
|
||
function useConfig() { | ||
const { currency, setCurrency, intlConfig, setIntlConfig } = | ||
useContext(ConfigContext); | ||
|
||
function handleCurrency(c: string) { | ||
setCurrency(c); | ||
setIntlConfig({ locale: optionsRepository.getUserLang(), currency: c }); | ||
} | ||
|
||
return { intlConfig, setIntlConfig, currency, handleCurrency }; | ||
} | ||
|
||
function ConfigProvider({ children }: PropsWithChildren) { | ||
const [currency, setCurrency] = useState<string>( | ||
optionsRepository.getDefaultCurrencyCode(), | ||
); | ||
const [intlConfig, setIntlConfig] = useState< | ||
CurrencyInputProps["intlConfig"] | ||
>({ | ||
locale: optionsRepository.getUserLang(), | ||
currency: currency, | ||
}); | ||
|
||
return ( | ||
<ConfigContext.Provider | ||
value={{ intlConfig, setIntlConfig, currency, setCurrency }} | ||
> | ||
{children} | ||
</ConfigContext.Provider> | ||
); | ||
} | ||
|
||
export { ConfigProvider, useConfig }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { cleanup, render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { act } from "react-dom/test-utils"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { describe, expect, it } from "vitest"; | ||
import { BudgetMother } from "../../domain/budget.mother"; | ||
import { | ||
budgetContextSpy, | ||
redoMock, | ||
setBudgetMock, | ||
setNotificationsMock, | ||
testBudgetContext, | ||
undoMock, | ||
} from "../../../setupTests"; | ||
import { BudgetPage } from "./BudgetPage"; | ||
import { localForageBudgetRepository } from "../../infrastructure/localForageBudgetRepository"; | ||
|
||
const budgetRepository = new localForageBudgetRepository(); | ||
|
||
describe("BudgetPage", () => { | ||
const comp = ( | ||
<BrowserRouter> | ||
<BudgetPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
it("matches snapshot", () => { | ||
render(comp); | ||
expect(comp).toMatchSnapshot(); | ||
}); | ||
|
||
it("renders initial state", async () => { | ||
render(comp); | ||
const newButton = screen.getAllByRole("button", { name: "new budget" }); | ||
await act(async () => { | ||
await userEvent.click(newButton[0]); | ||
}); | ||
expect(screen.getByLabelText("delete budget")).toBeInTheDocument(); | ||
}); | ||
|
||
it("responds to new budget keyboard shortcut", async () => { | ||
render(comp); | ||
await userEvent.type(await screen.findByTestId("header"), "a"); | ||
expect(setBudgetMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it("removes budget when clicking on delete budget button", async () => { | ||
render(comp); | ||
const deleteButton = await screen.findAllByRole("button", { | ||
name: "delete budget", | ||
}); | ||
await userEvent.click(deleteButton[0]); | ||
await userEvent.click( | ||
await screen.findByRole("button", { name: "confirm budget deletion" }), | ||
); | ||
await expect( | ||
budgetRepository.get(BudgetMother.testBudget().id), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it.skip("clones budget when clicking on clone budget button", async () => { | ||
render(comp); | ||
const newButton = await screen.findAllByRole("button", { | ||
name: "new budget", | ||
}); | ||
await userEvent.click(newButton[0]); | ||
|
||
const cloneButton = screen.getAllByRole("button", { | ||
name: "clone budget", | ||
}); | ||
await userEvent.click(cloneButton[0]); | ||
expect(setBudgetMock).toHaveBeenCalledWith( | ||
BudgetMother.testBudgetClone(), | ||
true, | ||
); | ||
}); | ||
|
||
it.skip("responds to clone budget keyboard shortcut", async () => { | ||
render(comp); | ||
const newButton = await screen.findAllByRole("button", { | ||
name: "new budget", | ||
}); | ||
await userEvent.click(newButton[0]); | ||
|
||
await userEvent.type(await screen.findByTestId("header"), "c"); | ||
expect(setBudgetMock).toHaveBeenCalledWith( | ||
BudgetMother.testBudgetClone(), | ||
true, | ||
); | ||
}); | ||
|
||
it("responds to undo change keyboard shortcut", async () => { | ||
cleanup(); | ||
budgetContextSpy.mockReturnValue({ ...testBudgetContext, canUndo: true }); | ||
render(comp); | ||
await userEvent.type(await screen.findByTestId("header"), "u"); | ||
expect(undoMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it("responds to redo change keyboard shortcut", async () => { | ||
cleanup(); | ||
budgetContextSpy.mockReturnValue({ ...testBudgetContext, canRedo: true }); | ||
render(comp); | ||
await userEvent.type(await screen.findByTestId("header"), "r"); | ||
expect(redoMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it("responds to clear notifications keyboard shortcut", async () => { | ||
render(comp); | ||
setNotificationsMock.mockClear(); | ||
await userEvent.type(await screen.findByTestId("header"), "{Escape}"); | ||
expect(setNotificationsMock).toHaveBeenCalledWith([]); | ||
}); | ||
|
||
it("responds to show graphs keyboard shortcut", async () => { | ||
render(comp); | ||
await userEvent.type(await screen.findByTestId("header"), "i"); | ||
expect(screen.getByRole("status")).toBeInTheDocument(); | ||
}); | ||
}); |