-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathCache.test.tsx
More file actions
191 lines (165 loc) · 5.88 KB
/
Cache.test.tsx
File metadata and controls
191 lines (165 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React from "react";
import { within, waitFor, fireEvent } from "@testing-library/react";
import { Cache } from "../Cache";
import { renderWithApolloClient } from "../../../utilities/testing/renderWithApolloClient";
import { client, currentClient, writeData } from "../../../index";
const CACHE_DATA = {
"Result:1": {
id: "1",
__typename: "Result",
name: "Result 1",
},
"Result:2": {
id: "2",
__typename: "Result",
name: "Result 2",
},
ROOT_QUERY: {
__typename: "Query",
search: {
__typename: "Results",
count: 2,
success: true,
error: null,
results: [
{
__ref: "Result:1",
},
{
__ref: "Result:2",
},
],
},
},
};
const navigationProps = {
queriesCount: 2,
mutationsCount: 0,
};
describe("Cache component tests", () => {
describe("No cache data", () => {
it("should show no cache data message in sidebar", () => {
const { getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const sidebar = getByTestId("sidebar");
return waitFor(() => {
expect(within(sidebar).getByText("No cache data")).toBeInTheDocument();
});
});
it("should leave the header blank instead of trying to show a cache ID", () => {
const { getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const header = getByTestId("header");
return waitFor(() => {
expect(header.firstChild).not.toBeInTheDocument();
});
});
});
describe("With cache data", () => {
beforeEach(() => {
const clientId = "client-1";
currentClient(clientId);
writeData({
id: clientId,
queries: [],
mutations: [],
cache: JSON.stringify(CACHE_DATA),
});
});
afterEach(() => {
client.resetStore();
});
it("should show list of root cache ids in the sidebar", () => {
const { getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const sidebar = getByTestId("sidebar");
return waitFor(() => {
expect(within(sidebar).getByText("ROOT_QUERY")).toBeInTheDocument();
expect(within(sidebar).getByText("Result:1")).toBeInTheDocument();
expect(within(sidebar).getByText("Result:2")).toBeInTheDocument();
});
});
it("should show sidebar selected/active cache ID in the header", () => {
const { getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const header = getByTestId("header");
return waitFor(() => {
expect(within(header).getByText("ROOT_QUERY")).toBeInTheDocument();
});
});
it("should show data for the sidebar selected/active cache ID in main ", () => {
const { getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const main = getByTestId("main");
expect(within(main).getByText("ROOT_QUERY")).toBeInTheDocument();
expect(within(main).getByText("__typename:")).toBeInTheDocument();
expect(within(main).getByText('"Query"')).toBeInTheDocument();
expect(within(main).getByText("search:")).toBeInTheDocument();
fireEvent.click(within(main).getByText("▶"));
expect(within(main).getByText("count:")).toBeInTheDocument();
expect(within(main).getByText("2")).toBeInTheDocument();
expect(within(main).getByText("success:")).toBeInTheDocument();
expect(within(main).getByText("true")).toBeInTheDocument();
expect(within(main).getByText("error:")).toBeInTheDocument();
expect(within(main).getByText("null")).toBeInTheDocument();
});
});
describe("Search", () => {
const selectedSidebarStyles = "background-color: #1B2240";
const selectedMainStyles = "background-color: yellow";
beforeEach(() => {
const clientId = "client-1";
currentClient(clientId);
writeData({
id: clientId,
queries: [],
mutations: [],
cache: JSON.stringify(CACHE_DATA),
});
});
it("should highlight sidebar cache ID's if a match is found", () => {
const { getByPlaceholderText, getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const searchInput = getByPlaceholderText("Search queries");
fireEvent.change(searchInput, { target: { value: "Result 2" } });
const sidebar = getByTestId("sidebar");
return waitFor(() => {
expect((searchInput as any).value).toBe("Result 2");
const result1 = within(sidebar).getByText("Result:1");
expect(result1.parentNode).not.toHaveStyle(selectedSidebarStyles);
const result2 = within(sidebar).getByText("Result:2");
expect(result2.parentNode).toHaveStyle(selectedSidebarStyles);
});
});
it("should highlight object keys/values if a match is found", () => {
const { getByPlaceholderText, getByTestId } = renderWithApolloClient(
<Cache navigationProps={navigationProps} />
);
const searchInput = getByPlaceholderText("Search queries");
fireEvent.change(searchInput, { target: { value: "Result 2" } });
const sidebar = getByTestId("sidebar");
const result2 = within(sidebar).getByText("Result:2");
const result2Parent = result2.parentNode;
fireEvent.click(result2Parent!);
const main = getByTestId("main");
return waitFor(() => {
expect(within(main).getByText("__typename:")).not.toHaveStyle(
selectedMainStyles
);
expect(within(main).getByText('"Result"')).not.toHaveStyle(
selectedMainStyles
);
expect(within(main).getByText("name:")).toHaveStyle(selectedMainStyles);
expect(within(main).getByText('"Result 2"')).toHaveStyle(
selectedMainStyles
);
});
});
});
});