Merge branch 'link-store'

This commit is contained in:
Yanick Champoux 2023-04-16 11:53:17 -04:00
commit 5f18ce9fe9
6 changed files with 47 additions and 12 deletions

View File

@ -25,7 +25,6 @@
export let rating = 0; export let rating = 0;
export let api = getContext("api"); export let api = getContext("api");
console.log(api?.dispatch?.setDrive);
$: api?.dispatch?.setDrive?.({ rating, advanced }); $: api?.dispatch?.setDrive?.({ rating, advanced });
</script> </script>

View File

@ -1,9 +1,20 @@
import ship from "./ship"; import ship from "./ship";
import { browser } from "$app/environment";
export type Api = ReturnType<typeof ship.createStore>; export type Api = ReturnType<typeof ship.createStore>;
export const createApi = () => { export const createApi = () => {
const api = ship.createStore(); const state = browser
console.log(api); ? JSON.parse(localStorage.getItem("ship") || "null")
: undefined;
const api = ship.createStore(state || undefined);
if (browser) {
api.subscribe(() => {
localStorage.setItem("ship", JSON.stringify(api.getState()));
});
}
return api; return api;
}; };

View File

@ -18,7 +18,9 @@ import { fireconsDux } from "./ship/weaponry/firecons";
import { adfcDux } from "./ship/weaponry/adfc"; import { adfcDux } from "./ship/weaponry/adfc";
import { weaponsDux } from "./ship/weaponry/weapons"; import { weaponsDux } from "./ship/weaponry/weapons";
if (typeof process !== "undefined") {
process.env.UPDEEP_MODE = "dangerously_never_freeze"; process.env.UPDEEP_MODE = "dangerously_never_freeze";
}
const structure = new Updux({ const structure = new Updux({
initialState: {}, initialState: {},
@ -98,11 +100,6 @@ shipDux.addReaction((api) => {
}; };
}); });
shipDux.addEffect((api) => (next) => (action) => {
console.log(action);
next(action);
});
shipDux.addReaction((api) => shipDux.addReaction((api) =>
createSelector( createSelector(
api.selectors.getShipMass, api.selectors.getShipMass,

1
src/routes/+layout.js Normal file
View File

@ -0,0 +1 @@
export const prerender = true;

View File

@ -1,5 +1,14 @@
<ShipEdit /> <ShipEdit {...ship} />
<script> <script>
import { getContext } from "svelte";
import ShipEdit from "$lib/components/ShipEdit.svelte"; import ShipEdit from "$lib/components/ShipEdit.svelte";
export let api = getContext("api");
let ship = {};
api?.subscribe(() => {
ship = api.getState();
});
</script> </script>

View File

@ -1,9 +1,27 @@
import { render, fireEvent, screen, getByText } from "@testing-library/svelte"; import { render, fireEvent } from "@testing-library/svelte";
import "@testing-library/jest-dom"; import "@testing-library/jest-dom";
import { tick } from "svelte";
import Page from "./+page.svelte"; import Page from "./+page.svelte";
import { createApi } from "$lib/store/api.ts";
test("we have a page", () => { test("we have a page", () => {
const { getByText } = render(Page); const { getByText } = render(Page);
expect(getByText("propulsion")).toBeInTheDocument(); expect(getByText("propulsion")).toBeInTheDocument();
}); });
test("we can pass a store", async () => {
const context = new Map();
const api = createApi();
context.set("api", api);
api.dispatch.updateIdentification({ shipClass: "Bonobo" });
const { getByPlaceholderText } = render(Page, { context });
const classInput = getByPlaceholderText("ship class");
await fireEvent.input(classInput, { target: { value: "Tarzan" } });
expect(classInput.value).toEqual("Tarzan");
expect(api.getState().identification.shipClass).toBe("Tarzan");
});