Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/crutchcorn/the-fun-framework. 🛝 An experimental homegrown JS framework. crutchcorn.github.io/the-fun-framework
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270import { describe, test, expect } from "vitest";
import userEvent from "@testing-library/user-event";import { createState, registerComponent, render } from "the-fun-framework";import { findByText, getByText, queryByText } from "@testing-library/dom";
const user = userEvent.setup();
describe("render", () => { test("should support rendering static values", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <p>{{message}}</p> </div> `;
function App() { return { message: "Hello, world", }; }
App.selector = "App";
registerComponent(App); render(); expect(await findByText(document.body, "Hello, world")).toBeTruthy(); });
test("should support rendered values and updating them", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <button data-on-click="updateCount()">Add</button> <p>Count: {{count.value}}</p> </div> `;
function App() { const count = createState(0);
function updateCount() { count.value++; }
return { count, updateCount, }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Count: 0")).toBeTruthy(); await user.click(getByText(document.body, "Add")); expect(await findByText(document.body, "Count: 1")).toBeTruthy(); });
test("should support conditionally rendering", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <button data-on-click="updateCount()">Add</button> <p data-if="count.value > 0">Count is greater than 0</p> </div> `;
function App() { const count = createState(0);
function updateCount() { count.value++; }
return { count, updateCount, }; }
App.selector = "App";
registerComponent(App); render();
expect( await queryByText(document.body, "Count is greater than 0") ).not.toBeTruthy(); user.click(getByText(document.body, "Add")); expect( await findByText(document.body, "Count is greater than 0") ).toBeTruthy(); });
test("should support conditionally rendering multiple items", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <button data-on-click="updateCount()">Add</button> <p data-if="count.value > 0">Count is greater than 0</p> <p data-if="count.value === 0">Count is 0</p> </div> `;
function App() { const count = createState(0);
function updateCount() { count.value++; }
return { count, updateCount, }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Count is 0")).toBeTruthy(); await user.click(getByText(document.body, "Add")); expect( await findByText(document.body, "Count is greater than 0") ).toBeTruthy(); });
test("should support for loop rendering", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <p data-for="item of items" data-key="item.key">{{item.val}}</p> </div> `;
function App() { return { items: [ { key: 1, val: "Hello" }, { key: 2, val: "Goodbye" }, ], }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Hello")).toBeTruthy(); expect(await findByText(document.body, "Goodbye")).toBeTruthy(); });
test("should support for loop rendering with other elements present", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <div>Before</div> <p data-for="item of items" data-key="item.key">{{item.val}}</p> <div>After</div> </div> `;
function App() { return { items: [ { key: 1, val: "Hello" }, { key: 2, val: "Goodbye" }, ], }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Before")).toBeTruthy(); expect(await findByText(document.body, "Hello")).toBeTruthy(); expect(await findByText(document.body, "Goodbye")).toBeTruthy(); expect(await findByText(document.body, "After")).toBeTruthy(); });
test("should support for loop rendering two loops side-by-side", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <p data-for="item of otheritems" data-key="item.key">{{item.val}}</p> <p data-for="item of items" data-key="item.key">{{item.val}}</p> </div> `;
function App() { return { items: [ { key: 1, val: "Hello" }, { key: 2, val: "Goodbye" }, ], otheritems: [ { key: 1, val: "Other" }, { key: 2, val: "One" }, ], }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Hello")).toBeTruthy(); expect(await findByText(document.body, "Goodbye")).toBeTruthy(); expect(await findByText(document.body, "Other")).toBeTruthy(); expect(await findByText(document.body, "One")).toBeTruthy(); });
test("should support for dynamic loop rendering", async () => { document.body.innerHTML = ` <div data-island-comp="App"> <div> <p data-for="person of people.value" data-key="person.key">{{person.name}}</p> </div> <button data-on-click="addPerson()">Add person</button> </div> `;
function App() { const people = createState([ { name: "Corbin", key: "corbin", }, { name: "Ade", key: "ade", }, ]);
let personCount = 0; function addPerson() { const newList = [...people.value]; ++personCount; newList.push({ name: `Person ${personCount}`, key: `person_${personCount}`, }); people.value = newList; }
return { people, addPerson, }; }
App.selector = "App";
registerComponent(App); render();
expect(await findByText(document.body, "Corbin")).toBeTruthy(); expect(await findByText(document.body, "Ade")).toBeTruthy(); await user.click(getByText(document.body, "Add person")); expect(await findByText(document.body, "Person 1")).toBeTruthy(); await user.click(getByText(document.body, "Add person")); expect(await findByText(document.body, "Person 2")).toBeTruthy(); await user.click(getByText(document.body, "Add person")); expect(await findByText(document.body, "Person 3")).toBeTruthy(); });});