|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { strict as assert } from "node:assert"; |
| 7 | + |
| 8 | +import type { ContainerSchema } from "@fluidframework/fluid-static"; |
| 9 | +import type { PropTreeNode } from "@fluidframework/react/alpha"; |
| 10 | +import { treeDataObject, TreeViewComponent } from "@fluidframework/react/alpha"; |
| 11 | +import { TinyliciousClient } from "@fluidframework/tinylicious-client"; |
| 12 | +import { SchemaFactory, TreeViewConfiguration } from "@fluidframework/tree"; |
| 13 | +// eslint-disable-next-line import-x/no-internal-modules |
| 14 | +import { independentView } from "@fluidframework/tree/internal"; |
| 15 | +import { render } from "@testing-library/react"; |
| 16 | +import globalJsdom from "global-jsdom"; |
| 17 | +import * as React from "react"; |
| 18 | + |
| 19 | +import { Inventory, Part, treeConfiguration } from "../schema.js"; |
| 20 | +import { MainView } from "../view/inventoryList.js"; |
| 21 | + |
| 22 | +describe("inventoryApp", () => { |
| 23 | + it("treeDataObject", async () => { |
| 24 | + const containerSchema = { |
| 25 | + initialObjects: { |
| 26 | + tree: treeDataObject( |
| 27 | + treeConfiguration, |
| 28 | + () => |
| 29 | + new Inventory({ |
| 30 | + parts: [ |
| 31 | + new Part({ name: "nut", quantity: 5 }), |
| 32 | + new Part({ name: "bolt", quantity: 6 }), |
| 33 | + ], |
| 34 | + }), |
| 35 | + ), |
| 36 | + }, |
| 37 | + } satisfies ContainerSchema; |
| 38 | + |
| 39 | + const tinyliciousClient = new TinyliciousClient(); |
| 40 | + |
| 41 | + const { container } = await tinyliciousClient.createContainer(containerSchema, "2"); |
| 42 | + const dataObject = container.initialObjects.tree; |
| 43 | + assert.equal(dataObject.treeView.root.parts.length, 2); |
| 44 | + const firstPart = dataObject.treeView.root.parts[0]; |
| 45 | + assert(firstPart !== undefined); |
| 46 | + firstPart.quantity += 1; |
| 47 | + assert.equal(dataObject.treeView.root.parts[0]?.quantity, 6); |
| 48 | + assert.equal(dataObject.treeView.root.parts[1]?.quantity, 6); |
| 49 | + |
| 50 | + container.dispose(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("dom tests", () => { |
| 54 | + let cleanup: () => void; |
| 55 | + |
| 56 | + before(() => { |
| 57 | + cleanup = globalJsdom(); |
| 58 | + }); |
| 59 | + |
| 60 | + after(() => { |
| 61 | + cleanup(); |
| 62 | + }); |
| 63 | + |
| 64 | + // Run without strict mode to make sure it works in a normal production setup. |
| 65 | + // Run with strict mode to potentially detect additional issues. |
| 66 | + for (const reactStrictMode of [false, true]) { |
| 67 | + describe(`StrictMode: ${reactStrictMode}`, () => { |
| 68 | + const builder = new SchemaFactory("tree-react-api"); |
| 69 | + |
| 70 | + class Item extends builder.object("Item", {}) {} |
| 71 | + |
| 72 | + const View = ({ root }: { root: PropTreeNode<Item> }): React.JSX.Element => ( |
| 73 | + <span>View</span> |
| 74 | + ); |
| 75 | + |
| 76 | + it("TreeViewComponent", () => { |
| 77 | + const view = independentView(new TreeViewConfiguration({ schema: Item })); |
| 78 | + const content = <TreeViewComponent viewComponent={View} tree={{ treeView: view }} />; |
| 79 | + const rendered = render(content, { reactStrictMode }); |
| 80 | + |
| 81 | + // Ensure that viewing an incompatible document displays an error. |
| 82 | + assert.match(rendered.baseElement.textContent ?? "", /Document is incompatible/); |
| 83 | + // Ensure that changes in compatibility are detected and invalidate the view, |
| 84 | + // and that compatible documents show the content from `viewComponent` |
| 85 | + view.initialize(new Item({})); |
| 86 | + rendered.rerender(content); |
| 87 | + assert.equal(rendered.baseElement.textContent, "View"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("renders MainView with inventory data", () => { |
| 91 | + const view = independentView(treeConfiguration); |
| 92 | + const content = ( |
| 93 | + <TreeViewComponent viewComponent={MainView} tree={{ treeView: view }} /> |
| 94 | + ); |
| 95 | + const rendered = render(content, { reactStrictMode }); |
| 96 | + |
| 97 | + // Ensure that viewing an incompatible document displays an error. |
| 98 | + assert.match(rendered.baseElement.textContent ?? "", /Document is incompatible/); |
| 99 | + |
| 100 | + // Initialize with inventory data |
| 101 | + view.initialize( |
| 102 | + new Inventory({ |
| 103 | + parts: [ |
| 104 | + new Part({ name: "bolt", quantity: 5 }), |
| 105 | + new Part({ name: "nut", quantity: 10 }), |
| 106 | + ], |
| 107 | + }), |
| 108 | + ); |
| 109 | + rendered.rerender(content); |
| 110 | + |
| 111 | + // Verify the app renders the inventory header and parts |
| 112 | + assert.match(rendered.baseElement.textContent ?? "", /Inventory:/); |
| 113 | + assert.match(rendered.baseElement.textContent ?? "", /bolt/); |
| 114 | + assert.match(rendered.baseElement.textContent ?? "", /nut/); |
| 115 | + }); |
| 116 | + }); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
0 commit comments