-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathArtistSavedSearch.tests.tsx
148 lines (122 loc) · 4.45 KB
/
ArtistSavedSearch.tests.tsx
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
import { fireEvent, screen } from "@testing-library/react-native"
import { flushPromiseQueue } from "app/utils/tests/flushPromiseQueue"
import { rejectMostRecentRelayOperation } from "app/utils/tests/rejectMostRecentRelayOperation"
import { renderWithHookWrappersTL } from "app/utils/tests/renderWithWrappers"
import { isEqual } from "lodash"
import { MockPayloadGenerator, createMockEnvironment } from "relay-test-utils"
import { MockResolvers } from "relay-test-utils/lib/RelayMockPayloadGenerator"
import { ArtistQueryRenderer } from "./Artist"
jest.unmock("react-tracking")
const mockUseIsFocusedMock = jest.fn()
const mockAddListener = jest.fn((event, callback) => {
if (event === "focus" || event === "blur") {
callback()
}
return jest.fn() // return a function to mimic the unsubscribe function
})
jest.mock("@react-navigation/native", () => {
const actualNav = jest.requireActual("@react-navigation/native")
return {
...actualNav,
useRoute: () => {
return {}
},
useNavigation: () => ({
addListener: mockAddListener,
navigate: jest.fn(),
}),
useIsFocused: () => mockUseIsFocusedMock(),
}
})
type ArtistQueries = "ArtistAboveTheFoldQuery" | "ArtistBelowTheFoldQuery" | "SearchCriteriaQuery"
describe("Saved search banner on artist screen", () => {
const originalError = console.error
const originalWarn = console.warn
let environment = createMockEnvironment()
beforeEach(() => {
environment = createMockEnvironment()
console.error = jest.fn()
console.warn = jest.fn()
})
afterEach(() => {
environment = createMockEnvironment()
console.error = originalError
console.warn = originalWarn
})
function mockMostRecentOperation(name: ArtistQueries, mockResolvers: MockResolvers = {}) {
expect(environment.mock.getMostRecentOperation().request.node.operation.name).toBe(name)
environment.mock.resolveMostRecentOperation((operation) => {
const result = MockPayloadGenerator.generate(operation, {
ID({ path }) {
// need to make sure artist id is stable between above-and-below-the-fold queries to avoid cache weirdness
if (isEqual(path, ["artist", "id"])) {
return "artist-id"
}
},
...mockResolvers,
})
return result
})
}
const getTree = (alertID?: string) =>
renderWithHookWrappersTL(
<ArtistQueryRenderer
artistID="ignore"
environment={environment}
alertID={alertID}
initialTab="Artworks"
/>
)
it("should convert the criteria attributes to the filter params format", async () => {
getTree("search-criteria-id")
mockMostRecentOperation("SearchCriteriaQuery", MockSearchCriteriaQuery)
mockMostRecentOperation("ArtistAboveTheFoldQuery", MockArtistAboveTheFoldQuery)
await flushPromiseQueue()
fireEvent.press(screen.getByText("Sort & Filter"))
expect(screen.getByText(/Sort By/)).toBeOnTheScreen()
expect(screen.getByText(/Rarity/)).toBeOnTheScreen()
expect(screen.getByText(/Ways to Buy/)).toBeOnTheScreen()
})
it("should an error message when something went wrong during the search criteria query", async () => {
getTree("something")
rejectMostRecentRelayOperation(environment, new Error())
mockMostRecentOperation("ArtistAboveTheFoldQuery", MockArtistAboveTheFoldQuery)
await flushPromiseQueue()
expect(screen.getByText("Sorry, an error occured")).toBeOnTheScreen()
expect(screen.getByText("Failed to get saved search criteria")).toBeOnTheScreen()
})
it("should render saved search component", async () => {
getTree("search-criteria-id")
mockMostRecentOperation("SearchCriteriaQuery", MockSearchCriteriaQuery)
mockMostRecentOperation("ArtistAboveTheFoldQuery", MockArtistAboveTheFoldQuery)
await flushPromiseQueue()
expect(screen.getAllByText("Create Alert")).not.toHaveLength(0)
})
})
const MockSearchCriteriaQuery: MockResolvers = {
Alert() {
return {
attributionClass: ["limited edition", "open edition"],
acquireable: true,
inquireableOnly: true,
offerable: null,
atAuction: null,
width: null,
height: null,
}
},
}
const MockArtistAboveTheFoldQuery: MockResolvers = {
Artist() {
return {
has_metadata: true,
counts: { articles: 0, related_artists: 0, artworks: 1, partner_shows: 0 },
auctionResultsConnection: {
totalCount: 0,
},
}
},
ArtistInsight() {
return { entities: ["test"] }
},
}