diff --git a/bun.lockb b/bun.lockb index ec8f01157..d82408f92 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/lib/contexts/CharactersContext/CharactersContext.tsx b/lib/contexts/CharactersContext/CharactersContext.tsx index 2e43a0d60..26071a0a1 100644 --- a/lib/contexts/CharactersContext/CharactersContext.tsx +++ b/lib/contexts/CharactersContext/CharactersContext.tsx @@ -19,8 +19,14 @@ export function useCharacters(props?: { localStorage: Storage }) { onMigration: CharacterFactory.migrate, }); - async function add(type: ICharacterTemplate): Promise { - const newCharacter = await CharacterFactory.make(type); + async function add(props: { + template?: ICharacterTemplate; + json?: any; + }): Promise { + const newCharacter = await CharacterFactory.make({ + template: props.template, + json: props.json, + }); entityManager.actions.setEntities((draft: Array) => { return [newCharacter, ...draft]; diff --git a/lib/contexts/CharactersContext/__tests__/CharactersContext.test.tsx b/lib/contexts/CharactersContext/__tests__/CharactersContext.test.tsx index 2b754f0ca..9cd6f81da 100644 --- a/lib/contexts/CharactersContext/__tests__/CharactersContext.test.tsx +++ b/lib/contexts/CharactersContext/__tests__/CharactersContext.test.tsx @@ -1,4 +1,5 @@ import { act, renderHook } from "@testing-library/react"; +import { expect } from "vitest"; import { DefaultTemplates } from "../../../domains/character/DefaultTemplates"; import { ICharacter } from "../../../domains/character/types"; import { useCharacters } from "../CharactersContext"; @@ -54,9 +55,9 @@ describe("useCharacters", () => { // WHEN I add a new character let newCharacter: ICharacter | undefined = undefined; await act(async () => { - newCharacter = await result.current.actions.add( - DefaultTemplates.FateCondensed - ); + newCharacter = await result.current.actions.add({ + json: DefaultTemplates.FateCondensed, + }); }); // THEN the character is added expect(result.current.state.characters.length).toEqual(1); @@ -80,14 +81,14 @@ describe("useCharacters", () => { expect(result.current.state.characters[0]).toEqual( expect.objectContaining({ id: playingCharacter!.id, - }) + }), ); expect(result.current.state.characters[1]).toEqual( expect.objectContaining({ id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated, name: "UPDATED NAME", - }) + }), ); act(() => { @@ -100,7 +101,7 @@ describe("useCharacters", () => { id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated, name: "UPDATED NAME", - }) + }), ); expect(result.current.state.characters[1]).toEqual(undefined); @@ -114,7 +115,7 @@ describe("useCharacters", () => { id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated, name: "UPDATED NAME", - }) + }), ); act(() => { @@ -127,7 +128,7 @@ describe("useCharacters", () => { id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated, name: "UPDATED NAME", - }) + }), ); act(() => { @@ -146,7 +147,7 @@ describe("useCharacters", () => { id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated, name: "UPDATED NAME", - }) + }), ); act(() => { @@ -164,7 +165,7 @@ describe("useCharacters", () => { id: newCharacter!.id, lastUpdated: newCharacter!.lastUpdated + 100, name: "new timestamp", - }) + }), ); act(() => { @@ -184,14 +185,14 @@ describe("useCharacters", () => { id: "new-id", name: "new character", lastUpdated: newCharacter!.lastUpdated + 100, - }) + }), ); expect(result.current.state.characters[1]).toEqual( expect.objectContaining({ id: newCharacter!.id, name: "new timestamp", lastUpdated: newCharacter!.lastUpdated + 100, - }) + }), ); }); }); diff --git a/lib/domains/character/CharacterFactory.test.tsx b/lib/domains/character/CharacterFactory.test.tsx index 7a9964769..129ba6a25 100644 --- a/lib/domains/character/CharacterFactory.test.tsx +++ b/lib/domains/character/CharacterFactory.test.tsx @@ -1,4 +1,5 @@ import { produce } from "immer"; +import { expect } from "vitest"; import { CharacterFactory } from "./CharacterFactory"; import { DefaultTemplates } from "./DefaultTemplates"; import { ComplexCharacter } from "./mocks/ComplexCharacter"; @@ -654,9 +655,9 @@ describe("CharacterFactory.migrate", () => { describe("CharacterFactory.duplicate", () => { it("should reset the ids", async () => { - const defaultCharacter = await CharacterFactory.make( - DefaultTemplates.FateCondensed, - ); + const defaultCharacter = await CharacterFactory.make({ + json: DefaultTemplates.FateCondensed, + }); const characterWithFakeIds = produce(defaultCharacter, (draft) => { draft.id = "1"; diff --git a/lib/domains/character/CharacterFactory.tsx b/lib/domains/character/CharacterFactory.tsx index dbb5efcb0..6add5bc28 100644 --- a/lib/domains/character/CharacterFactory.tsx +++ b/lib/domains/character/CharacterFactory.tsx @@ -32,8 +32,13 @@ import { export const CharacterFactory = { latestVersion: 4, - async make(template: ICharacterTemplate): Promise { - const result = await fetch(template.fetchPath).then((r) => r.json()); + async make(props: { + template?: ICharacterTemplate; + json?: any; + }): Promise { + let result = props.template + ? await fetch(props.template.fetchPath).then((r) => r.json()) + : props.json; const newCharacter = this.makeFromJson(result); const characterWithNewName = { ...newCharacter, diff --git a/lib/domains/character/DefaultTemplates.ts b/lib/domains/character/DefaultTemplates.ts index 3d0dc83c1..ac6db3dda 100644 --- a/lib/domains/character/DefaultTemplates.ts +++ b/lib/domains/character/DefaultTemplates.ts @@ -1,20 +1,8 @@ -import { ICharacterTemplate } from "../../services/character-templates/CharacterTemplateService"; - export const DefaultTemplates = { - BlankTemplate: { - publisher: "Default", - name: "Blank", - fetchPath: "/public/character-templates/Blank/Blank.json", - } as ICharacterTemplate, - FateCondensed: { - publisher: "Default", - name: "FateCondensed", - fetchPath: "/public/character-templates/Fate Condensed/Fate Condensed.json", - FateAccelerated: { - publisher: "Default", - name: "FateAccelerated", - fetchPath: - "/public/character-templates/Fate Accelerated/Fate Accelerated.json", - } as ICharacterTemplate, - }, + BlankTemplate: await import( + "../../../public/character-templates/Defaults/Blank.json" + ), + FateCondensed: await import( + "../../../public/character-templates/Fate Condensed/Fate Condensed.json" + ), } as const; diff --git a/lib/routes/Character/hooks/__tests__/useCharacter.test.tsx b/lib/routes/Character/hooks/__tests__/useCharacter.test.tsx index 645647da3..9abea7f4b 100644 --- a/lib/routes/Character/hooks/__tests__/useCharacter.test.tsx +++ b/lib/routes/Character/hooks/__tests__/useCharacter.test.tsx @@ -2,6 +2,7 @@ * @jest-environment jsdom */ import { act, renderHook, waitFor } from "@testing-library/react"; +import { expect } from "vitest"; import { CharacterFactory } from "../../../../domains/character/CharacterFactory"; import { DefaultTemplates } from "../../../../domains/character/DefaultTemplates"; import { ICharacter } from "../../../../domains/character/types"; @@ -10,9 +11,9 @@ import { useCharacter } from "../useCharacter"; describe("useCharacter", () => { describe("sanitizeCharacter", () => { it("should sanitize the character", async () => { - const defaultCahracter = await CharacterFactory.make( - DefaultTemplates.FateCondensed - ); + const defaultCahracter = await CharacterFactory.make({ + json: DefaultTemplates.FateCondensed, + }); // GIVEN const character = { ...defaultCahracter, @@ -26,7 +27,7 @@ describe("useCharacter", () => { }, { initialProps: { character: character }, - } + }, ); // THEN expect(result.current.state.character).toEqual(character); @@ -35,10 +36,10 @@ describe("useCharacter", () => { result.current.actions.setName("Luke Skywalker"); }); expect( - result.current.actions.getCharacterWithNewTimestamp().name + result.current.actions.getCharacterWithNewTimestamp().name, ).toEqual("Luke Skywalker"); expect( - result.current.actions.getCharacterWithNewTimestamp().lastUpdated + result.current.actions.getCharacterWithNewTimestamp().lastUpdated, ).not.toEqual(1); }); }); @@ -54,7 +55,7 @@ describe("useCharacter", () => { }, { initialProps: { character: initialRenderCharacter }, - } + }, ); expect(result.current.state.character).toEqual(undefined); }); @@ -71,7 +72,7 @@ describe("useCharacter", () => { }, { initialProps: { character: initialRenderCharacter }, - } + }, ); // CHARACTER UPDATED IN DIALOG const newName = "Luke Skywalker"; @@ -101,7 +102,7 @@ describe("useCharacter", () => { }, { initialProps: { character: initialRenderCharacter }, - } + }, ); const newName = "Luke Skywalker"; act(() => { @@ -132,7 +133,7 @@ describe("useCharacter", () => { }, { initialProps: { character: initialRenderCharacter }, - } + }, ); // CHARACTER UPDATED IN DIALOG const newName = "Luke Skywalker"; @@ -161,7 +162,7 @@ describe("useCharacter", () => { }, { initialProps: { character: initialRenderCharacter }, - } + }, ); // CHARACTER UPDATED IN DIALOG const newName = "Luke Skywalker"; @@ -181,9 +182,9 @@ describe("useCharacter", () => { describe("load template", () => { it("should load the new template but keep the id and the name as is", async () => { - const defaultCharacter = await CharacterFactory.make( - DefaultTemplates.FateCondensed - ); + const defaultCharacter = await CharacterFactory.make({ + json: DefaultTemplates.FateCondensed, + }); // GIVEN const character = { ...defaultCharacter, @@ -198,7 +199,7 @@ describe("useCharacter", () => { }, { initialProps: { character: character }, - } + }, ); // THEN @@ -206,13 +207,15 @@ describe("useCharacter", () => { // WHEN a template is loading act(() => { - result.current.actions.loadTemplate(DefaultTemplates.FateAccelerated); + result.current.actions.loadTemplate({ + json: DefaultTemplates.FateAccelerated, + }); }); // Wait for JSON download await waitFor(() => { expect(result.current.state.character?.lastUpdated).not.toEqual( - character.lastUpdated + character.lastUpdated, ); }); diff --git a/package.json b/package.json index 03b0de929..50ff143fa 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@tldraw/tldraw": "^1.29.2", "@types/hoist-non-react-statics": "^3.3.1", "@types/node": "^20.4.8", + "@vitejs/plugin-react": "^4.0.4", "canvas-confetti": "^1.6.0", "dayjs": "^1.11.9", "dompurify": "^3.0.5", diff --git a/vite.config.ts b/vite.config.ts index 7c1b19a18..e6cfcb550 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,3 @@ -import legacy from "@vitejs/plugin-legacy"; import react from "@vitejs/plugin-react"; import { defineConfig } from "vitest/config"; @@ -6,16 +5,11 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ define: { "process.env.npm_package_version": JSON.stringify( - process.env.npm_package_version + process.env.npm_package_version, ), "process.env.JEST_WORKER_ID": JSON.stringify(false), }, - plugins: [ - react(), - legacy({ - targets: ["defaults", "not IE 11"], - }), - ], + plugins: [react()], server: { port: 1234, proxy: {