|
| 1 | +import * as React from "react"; |
| 2 | +import { useContext, FunctionComponent } from "react"; |
| 3 | +import { render } from "@testing-library/react"; |
| 4 | +import { |
| 5 | + createServerContext, |
| 6 | + DataContext, |
| 7 | + InternalContext, |
| 8 | +} from "../src/useSSE"; |
| 9 | + |
| 10 | +declare global { |
| 11 | + namespace NodeJS { |
| 12 | + interface Global { |
| 13 | + window: any; |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("createServerContext", () => { |
| 19 | + const createCustomElement = (check: Function) => { |
| 20 | + const CustomElement: FunctionComponent = () => { |
| 21 | + const data = useContext(DataContext); |
| 22 | + const internal = useContext(InternalContext); |
| 23 | + check(data, internal); |
| 24 | + return <div></div>; |
| 25 | + }; |
| 26 | + return CustomElement; |
| 27 | + }; |
| 28 | + |
| 29 | + test("should create ServerDataContext with initial data", (done) => { |
| 30 | + const check = (data: any, internal: any) => { |
| 31 | + expect(data).toEqual({}); |
| 32 | + expect(internal).toEqual({ requests: [], resolved: false, current: 0 }); |
| 33 | + done(); |
| 34 | + }; |
| 35 | + |
| 36 | + const CustomElement = createCustomElement(check); |
| 37 | + const { ServerDataContext } = createServerContext(); |
| 38 | + |
| 39 | + render( |
| 40 | + <ServerDataContext> |
| 41 | + <CustomElement /> |
| 42 | + </ServerDataContext> |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + test("element should be able to add request to context", (done) => { |
| 47 | + const check = (data: any, internal: any) => { |
| 48 | + expect(data).toEqual({}); |
| 49 | + |
| 50 | + internal.requests.push( |
| 51 | + () => |
| 52 | + new Promise((resolve) => { |
| 53 | + resolve(); |
| 54 | + }) |
| 55 | + ); |
| 56 | + |
| 57 | + expect(internal.requests.length).toBe(1); |
| 58 | + done(); |
| 59 | + }; |
| 60 | + const CustomElement = createCustomElement(check); |
| 61 | + const { ServerDataContext } = createServerContext(); |
| 62 | + render( |
| 63 | + <ServerDataContext> |
| 64 | + <CustomElement /> |
| 65 | + </ServerDataContext> |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test("element should be able to add request to context and modify context", async (done) => { |
| 70 | + const check = (data: any, internal: any) => { |
| 71 | + internal.requests.push( |
| 72 | + new Promise((resolve) => { |
| 73 | + data["my_key"] = "123"; |
| 74 | + resolve(); |
| 75 | + }) |
| 76 | + ); |
| 77 | + }; |
| 78 | + const CustomElement = createCustomElement(check); |
| 79 | + const { resolveData, ServerDataContext } = createServerContext(); |
| 80 | + render( |
| 81 | + <ServerDataContext> |
| 82 | + <CustomElement /> |
| 83 | + </ServerDataContext> |
| 84 | + ); |
| 85 | + let reply = await resolveData(); |
| 86 | + expect(reply.data).toEqual({ my_key: "123" }); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + |
| 90 | + test("data.toHtml() should return html with defualt global variable name", async (done) => { |
| 91 | + const check = (data: any, internal: any) => { |
| 92 | + internal.requests.push( |
| 93 | + new Promise((resolve) => { |
| 94 | + data["my_key"] = "123"; |
| 95 | + resolve(); |
| 96 | + }) |
| 97 | + ); |
| 98 | + }; |
| 99 | + const CustomElement = createCustomElement(check); |
| 100 | + const { resolveData, ServerDataContext } = createServerContext(); |
| 101 | + render( |
| 102 | + <ServerDataContext> |
| 103 | + <CustomElement /> |
| 104 | + </ServerDataContext> |
| 105 | + ); |
| 106 | + let reply = await resolveData(); |
| 107 | + expect(reply.toHtml()).toEqual( |
| 108 | + `<script>window._initialDataContext = ${JSON.stringify({ |
| 109 | + my_key: "123", |
| 110 | + })};</script>` |
| 111 | + ); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + |
| 115 | + test("data.toHtml() should return html with specific global variable name", async (done) => { |
| 116 | + const check = (data: any, internal: any) => { |
| 117 | + internal.requests.push( |
| 118 | + new Promise((resolve) => { |
| 119 | + data["my_key"] = "123"; |
| 120 | + resolve(); |
| 121 | + }) |
| 122 | + ); |
| 123 | + }; |
| 124 | + const CustomElement = createCustomElement(check); |
| 125 | + const { resolveData, ServerDataContext } = createServerContext(); |
| 126 | + render( |
| 127 | + <ServerDataContext> |
| 128 | + <CustomElement /> |
| 129 | + </ServerDataContext> |
| 130 | + ); |
| 131 | + let reply = await resolveData(); |
| 132 | + expect(reply.toHtml("my_global_variable")).toEqual( |
| 133 | + `<script>window.my_global_variable = ${JSON.stringify({ |
| 134 | + my_key: "123", |
| 135 | + })};</script>` |
| 136 | + ); |
| 137 | + done(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments