@openmrs/esm-form-builder-app 1.0.1-pre.140 → 1.0.1-pre.143

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  OpenMRS esm form builder provides an interactive interface that allows OpenMRS to build schemas effortlessly including all features of Ampath Form Builder. Users can create schemas using the interactive form builder or by writing JSON in the Schema editor.
4
4
 
5
+ ## Form Builder User Guide
6
+ See the thorough User Guide for the Form Builder here: https://ampath-forms.vercel.app/docs/quickstart
7
+ Prerequisites & dependencies are covered here: https://ampath-forms.vercel.app/docs/developer-guide/run-form-engine-in-openmrs3#prerequisites
8
+
5
9
  ## Running this code
6
10
 
7
11
  ```sh
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-builder-app",
3
- "version": "1.0.1-pre.140",
3
+ "version": "1.0.1-pre.143",
4
4
  "license": "MPL-2.0",
5
5
  "description": "OpenMRS ESM Form Builder App",
6
6
  "browser": "dist/openmrs-esm-form-builder-app.js",
@@ -0,0 +1,208 @@
1
+ import React from "react";
2
+ import { screen } from "@testing-library/react";
3
+ import userEvent from "@testing-library/user-event";
4
+ import { navigate, openmrsFetch } from "@openmrs/esm-framework";
5
+ import { renderWithSwr, waitForLoadingToFinish } from "../../test-helpers";
6
+ import Dashboard from "./dashboard.component";
7
+
8
+ const mockedOpenmrsFetch = openmrsFetch as jest.Mock;
9
+
10
+ const formsResponse = [
11
+ {
12
+ uuid: "2ddde996-b1c3-37f1-a53e-378dd1a4f6b5",
13
+ name: "Test Form 1",
14
+ encounterType: {
15
+ uuid: "dd528487-82a5-4082-9c72-ed246bd49591",
16
+ name: "Consultation",
17
+ },
18
+ version: "1",
19
+ published: true,
20
+ retired: false,
21
+ resources: [
22
+ {
23
+ uuid: "ea27fd4f-7a4d-4869-8855-5b890c8fed56",
24
+ name: "JSON schema",
25
+ dataType: "AmpathJsonSchema",
26
+ valueReference: "511efba8-f08f-4544-a6da-6a6fa2497b9e",
27
+ },
28
+ ],
29
+ },
30
+ ];
31
+
32
+ describe("Dashboard", () => {
33
+ it("renders an empty state view if no forms are available", async () => {
34
+ mockedOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } });
35
+
36
+ renderDashboard();
37
+
38
+ await waitForLoadingToFinish();
39
+
40
+ expect(
41
+ screen.getByRole("heading", { name: /form builder/i })
42
+ ).toBeInTheDocument();
43
+ expect(screen.getByRole("heading", { name: /forms/i })).toBeInTheDocument();
44
+ expect(screen.getByTitle(/empty data illustration/i)).toBeInTheDocument();
45
+ expect(
46
+ screen.getByText(/there are no forms to display/i)
47
+ ).toBeInTheDocument();
48
+ expect(screen.getByText(/create a new form/i)).toBeInTheDocument();
49
+ });
50
+
51
+ it("renders a list of forms fetched from the server", async () => {
52
+ mockedOpenmrsFetch.mockReturnValueOnce({
53
+ data: {
54
+ results: formsResponse,
55
+ },
56
+ });
57
+
58
+ renderDashboard();
59
+
60
+ await waitForLoadingToFinish();
61
+
62
+ expect(
63
+ screen.getByRole("heading", { name: /form builder/i })
64
+ ).toBeInTheDocument();
65
+ expect(
66
+ screen.getByRole("button", { name: /filter by publish status/i })
67
+ ).toBeInTheDocument();
68
+ expect(
69
+ screen.getByRole("button", { name: /create a new form/i })
70
+ ).toBeInTheDocument();
71
+ expect(
72
+ screen.getByRole("button", { name: /edit schema/i })
73
+ ).toBeInTheDocument();
74
+ expect(
75
+ screen.getByRole("button", { name: /download schema/i })
76
+ ).toBeInTheDocument();
77
+ expect(
78
+ screen.getByRole("search", { name: /filter table/i })
79
+ ).toBeInTheDocument();
80
+ expect(screen.getByRole("table")).toBeInTheDocument();
81
+ expect(screen.getByText(/Test Form 1/i)).toBeInTheDocument();
82
+ });
83
+
84
+ it("searching for a form by name filters the list of forms", async () => {
85
+ const user = userEvent.setup();
86
+
87
+ mockedOpenmrsFetch.mockReturnValueOnce({
88
+ data: {
89
+ results: formsResponse,
90
+ },
91
+ });
92
+
93
+ renderDashboard();
94
+
95
+ await waitForLoadingToFinish();
96
+
97
+ expect(screen.getByText(/Test Form 1/i)).toBeInTheDocument();
98
+
99
+ const searchbox = screen.getByRole("searchbox");
100
+
101
+ await user.type(searchbox, "COVID");
102
+
103
+ expect(screen.queryByText(/Test Form 1/i)).not.toBeInTheDocument();
104
+ expect(
105
+ screen.getByText(/no matching forms to display/i)
106
+ ).toBeInTheDocument();
107
+ });
108
+
109
+ it('filters the list of forms by "published" status', async () => {
110
+ const user = userEvent.setup();
111
+
112
+ mockedOpenmrsFetch.mockReturnValueOnce({
113
+ data: {
114
+ results: formsResponse,
115
+ },
116
+ });
117
+
118
+ renderDashboard();
119
+
120
+ await waitForLoadingToFinish();
121
+
122
+ const publishStatusFilter = screen.getByRole("button", {
123
+ name: /filter by publish status/i,
124
+ });
125
+
126
+ await user.click(publishStatusFilter);
127
+ await user.click(screen.getByRole("option", { name: /unpublished/i }));
128
+
129
+ expect(screen.queryByText(/Test Form 1/i)).not.toBeInTheDocument();
130
+ expect(
131
+ screen.getByText(/no matching forms to display/i)
132
+ ).toBeInTheDocument();
133
+ });
134
+
135
+ it('clicking on "create a new form" button navigates to the "create form" page', async () => {
136
+ const user = userEvent.setup();
137
+
138
+ mockedOpenmrsFetch.mockReturnValueOnce({
139
+ data: {
140
+ results: formsResponse,
141
+ },
142
+ });
143
+
144
+ renderDashboard();
145
+
146
+ await waitForLoadingToFinish();
147
+
148
+ const createFormButton = screen.getByRole("button", {
149
+ name: /create a new form/i,
150
+ });
151
+
152
+ await user.click(createFormButton);
153
+
154
+ expect(navigate).toHaveBeenCalledWith({
155
+ to: expect.stringMatching(/form\-builder\/new/),
156
+ });
157
+ });
158
+
159
+ it('clicking on "edit schema" button navigates to the "edit schema" page', async () => {
160
+ const user = userEvent.setup();
161
+
162
+ mockedOpenmrsFetch.mockReturnValueOnce({
163
+ data: {
164
+ results: formsResponse,
165
+ },
166
+ });
167
+
168
+ renderDashboard();
169
+
170
+ await waitForLoadingToFinish();
171
+
172
+ const editSchemaButton = screen.getByRole("button", {
173
+ name: /edit schema/i,
174
+ });
175
+
176
+ await user.click(editSchemaButton);
177
+
178
+ expect(navigate).toHaveBeenCalledWith({
179
+ to: expect.stringMatching(/form\-builder\/edit/),
180
+ });
181
+ });
182
+
183
+ it('clicking on "download schema" button downloads the schema', async () => {
184
+ const user = userEvent.setup();
185
+
186
+ mockedOpenmrsFetch.mockReturnValueOnce({
187
+ data: {
188
+ results: formsResponse,
189
+ },
190
+ });
191
+
192
+ renderDashboard();
193
+
194
+ await waitForLoadingToFinish();
195
+
196
+ const downloadSchemaButton = screen.getByRole("button", {
197
+ name: /download schema/i,
198
+ });
199
+
200
+ await user.click(downloadSchemaButton);
201
+
202
+ expect(window.URL.createObjectURL).toHaveBeenCalled();
203
+ });
204
+ });
205
+
206
+ function renderDashboard() {
207
+ renderWithSwr(<Dashboard />);
208
+ }
@@ -1 +1,11 @@
1
1
  import "@testing-library/jest-dom/extend-expect";
2
+
3
+ declare global {
4
+ interface Window {
5
+ URL: {
6
+ createObjectURL: (blob: Blob) => string;
7
+ };
8
+ }
9
+ }
10
+
11
+ window.URL.createObjectURL = jest.fn();
@@ -0,0 +1,37 @@
1
+ import React from "react";
2
+ import { SWRConfig } from "swr";
3
+ import {
4
+ render,
5
+ screen,
6
+ waitForElementToBeRemoved,
7
+ } from "@testing-library/react";
8
+
9
+ // This component wraps whatever component is passed to it with an SWRConfig context which provides a global configuration for all SWR hooks.
10
+ const swrWrapper = ({ children }) => {
11
+ return (
12
+ <SWRConfig
13
+ value={{
14
+ // Sets the `dedupingInterval` to 0 - we don't need to dedupe requests in our test environment.
15
+ dedupingInterval: 0,
16
+ // Returns a new Map object, effectively wrapping our application with an empty cache provider. This is useful for resetting the SWR cache between test cases.
17
+ provider: () => new Map(),
18
+ }}
19
+ >
20
+ {children}
21
+ </SWRConfig>
22
+ );
23
+ };
24
+
25
+ // Render the provided component within the wrapper we created above
26
+ export const renderWithSwr = (ui, options?) =>
27
+ render(ui, { wrapper: swrWrapper, ...options });
28
+
29
+ // Helper function that waits for a loading state to disappear from the screen
30
+ export function waitForLoadingToFinish() {
31
+ return waitForElementToBeRemoved(
32
+ () => [...screen.queryAllByRole(/progressbar/i)],
33
+ {
34
+ timeout: 4000,
35
+ }
36
+ );
37
+ }