forked from JetBrains/guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.test.tsx
More file actions
168 lines (150 loc) · 4.9 KB
/
explore.test.tsx
File metadata and controls
168 lines (150 loc) · 4.9 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
import { beforeEach, describe, expect, test } from "vitest";
import { screen } from "@testing-library/dom";
// @ts-ignore
import { ExploreViewModel, renderCards } from "../public/assets/js/evm";
import fixtures, { baseRenderData } from "../_includes/fixtures";
import { ChannelHomepageData } from "../_includes/resources/channel/ChannelModels";
// @ts-ignore
import ExplorePage from "./explore.11ty";
import { HTMLInputElement } from "happy-dom";
import { Resource } from "../src/ResourceModels";
import { jsxToString } from "jsx-async-runtime";
const lunrResources = fixtures.resources.map((resource) => {
return {
title: resource.title,
channel: "anotherchannel",
resourceType: resource.resourceType,
topics: ["topicx", "topicy", "topicz"],
};
});
lunrResources[0].channel = "python";
lunrResources[1].channel = "python";
lunrResources[0].topics = ["topic100", "topic200"];
lunrResources[2].channel = "django";
let cardTemplate,
facetMenuNode,
listingNode,
latestContent,
noResults,
evm: ExploreViewModel;
describe("Faceted Browse", () => {
const channelItem = fixtures.channelItems[0];
const pageLayoutData: ChannelHomepageData = {
...baseRenderData,
...channelItem.data,
page: channelItem.page,
};
const rm = fixtures.resourceMap;
const context = {
...fixtures.context,
getResource: () => rm.get("/tips/some-tip/") as Resource,
};
let explorePage: ExplorePage;
beforeEach(async () => {
explorePage = new ExplorePage();
document.body.innerHTML = await jsxToString(
explorePage.render.call(context, pageLayoutData),
);
cardTemplate = document.getElementById("cardTemplate");
facetMenuNode = document.getElementById("facetMenu");
listingNode = document.getElementById("listing");
latestContent = document.getElementById("latest-content");
noResults = document.getElementById("listing-no-results");
evm = new ExploreViewModel(
cardTemplate,
facetMenuNode,
listingNode,
lunrResources,
latestContent,
noResults,
);
});
test("construct view model", () => {
expect(evm.templateNode).toBeTruthy();
expect(evm.facetMenuNode).toBeTruthy();
expect(evm.listingNode).toBeTruthy();
expect(evm.latestContent).toBeTruthy();
expect(evm.noResults).toBeTruthy();
expect(evm.lunrResources.length).to.equal(lunrResources.length);
});
test("clicking a menu item toggles it", () => {
// @ts-ignore
const target = screen.getAllByRole("checkbox")[0] as HTMLInputElement;
expect(target.checked).not.to.be.true;
target.click();
expect(target.checked).to.be.true;
target.click();
expect(target.checked).not.to.be.true;
});
test("get initial checked facets", () => {
let facets = evm.getSelectedFacets();
expect(facets).to.be.null;
// Mark all as checked
evm.facetMenuNode
.querySelectorAll("input")
.forEach((input: HTMLInputElement) => (input.checked = true));
facets = evm.getSelectedFacets();
expect(facets.channels.length).to.be.greaterThan(2);
});
test("render initial json resources", () => {
evm.renderCards(lunrResources);
const resourceCards = screen
.getAllByRole("link")
.filter((it) => it.className.indexOf("title") >= 0);
expect(resourceCards.length).to.equal(lunrResources.length);
expect(resourceCards[0].textContent).to.equal(lunrResources[0].title);
expect(resourceCards[21].textContent).to.equal(lunrResources[21].title);
});
test("filter one facet group, one facet", () => {
const selectedFacets = {
channels: ["python"],
topics: [],
resources: [],
};
const filteredResources = evm.filterResources(selectedFacets);
expect(filteredResources.length).to.equal(2);
});
test("filter two facet groups, one facet", () => {
const selectedFacets = {
channels: ["python"],
topics: ["topic100"],
resources: ["tip"],
};
const filteredResources = evm.filterResources(selectedFacets);
expect(filteredResources.length).to.equal(1);
});
test("filter once channel, one topic, one resource", () => {
const selectedFacets = {
channels: ["python"],
topics: ["topic100"],
resources: ["tip"],
};
const filteredResources = evm.filterResources(selectedFacets);
expect(filteredResources.length).to.equal(1);
});
test("Test full explore template", () => {
expect(screen.getByTitle("Channels Group")).toBeTruthy();
expect(screen.getByTitle("Resources Group")).toBeTruthy();
cardTemplate = document.getElementById("cardTemplate");
facetMenuNode = document.getElementById("facetMenu");
listingNode = document.getElementById("listing");
evm = new ExploreViewModel(
cardTemplate,
facetMenuNode,
listingNode,
lunrResources,
);
// Click something, see if it is selected
if (facetMenuNode) {
const target = facetMenuNode.querySelectorAll("a[data-facet-value]")[0];
if (target) {
// @ts-ignore
target.click();
expect(target.classList.contains("is-active")).to.be.true;
// @ts-ignore
target.click();
expect(target.classList.contains("is-active")).not.to.be.true;
}
}
});
});