-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathSavedAlbumRow.test.tsx
More file actions
124 lines (96 loc) · 4.1 KB
/
SavedAlbumRow.test.tsx
File metadata and controls
124 lines (96 loc) · 4.1 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
import React from "react"
import "i18n/config"
import { render, screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { setupServer } from "msw/node"
import FileSaver from "file-saver"
import SavedAlbumRow from "./SavedAlbumRow"
import "../icons"
import { handlerCalled, handlers } from "../mocks/handlers"
const server = setupServer(...handlers)
// Mock out Bugsnag calls
jest.mock('@bugsnag/js')
server.listen({
onUnhandledRequest: 'warn'
})
beforeAll(() => {
// @ts-ignore
global.Blob = function (content, options) { return ({ content, options }) }
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
})
const { location } = window
beforeAll(() => {
// @ts-ignore
delete window.location
})
afterAll(() => {
window.location = location
})
afterEach(() => {
jest.restoreAllMocks()
server.resetHandlers()
})
const baseAlbumHeaders = '"Album URI","Album Name","Album Type","Album Artist URI(s)","Album Artist Name(s)","Album Release Date","Release Date Precision","Track Count","Saved At"'
// Use a snapshot test to ensure exact component rendering
test("saved album row loading", async () => {
const { asFragment } = render(<SavedAlbumRow accessToken="TEST_ACCESS_TOKEN" />)
expect(await screen.findByText(/Saved albums/)).toBeInTheDocument()
expect(asFragment()).toMatchSnapshot();
})
test("redirecting when access token is invalid", async () => {
// @ts-ignore
window.location = { pathname: "/exportify", href: "http://www.example.com/exportify" }
render(<SavedAlbumRow accessToken="INVALID_ACCESS_TOKEN" />)
await waitFor(() => {
expect(window.location.href).toBe("/exportify")
})
})
test("standard case exports successfully", async () => {
const saveAsMock = jest.spyOn(FileSaver, "saveAs")
saveAsMock.mockImplementation(jest.fn())
render(<SavedAlbumRow accessToken="TEST_ACCESS_TOKEN" />);
expect(await screen.findByText(/Saved albums/)).toBeInTheDocument()
const buttonElement = screen.getByRole("button", { name: /export/i })
expect(buttonElement).toBeInTheDocument()
await userEvent.click(buttonElement)
await waitFor(() => {
expect(buttonElement).toHaveAttribute("disabled")
})
await waitFor(() => {
expect(handlerCalled.mock.calls).toContainEqual(
['https://api.spotify.com/v1/me/albums?limit=50&offset=0']
)
})
await waitFor(() => {
expect(saveAsMock).toHaveBeenCalledTimes(1)
})
expect(saveAsMock).toHaveBeenCalledWith(
{
content: [
`${baseAlbumHeaders}\n` +
`"spotify:album:4iwv7b8gDPKztLkKCbWyhi","Best of Six By Seven","album","spotify:artist:4TXdHyuAOl3rAOFmZ6MeKz","Six by Seven","2017-02-17","day","14","2020-07-19T09:24:39Z"\n` +
`"spotify:album:4MxbRuLNbxf0RERbT8OHsU","Cinder","album","spotify:artist:6H9oDpJUDuw3nkogwhd21s","Lux Terminus","2025-04-18","day","10","2025-04-23T14:51:45Z"\n` +
`"spotify:album:7aIEHWiuOkDywdjyQyt8CL","program music II","album","spotify:artist:5sGsy5o8hBSMmDUFTC5Q2P","KASHIWA Daisuke","2016-04-30","day","8","2025-11-25T18:26:03Z"\n` +
`"spotify:album:3xOcExpIWzroZldcdc212q","The Overview","album","spotify:artist:4X42BfuhWCAZ2swiVze9O0","Steven Wilson","2025-03-14","day","12","2025-03-20T21:24:38Z"\n` +
`"spotify:album:6azzagF3oeYffG22gIiLWz","Nocturne","album","spotify:artist:2SDGIFzEh9xmE5zDKcMRkj","The Human Abstract","2006-08-22","day","12","2025-11-25T00:41:40Z"\n` +
`"spotify:album:4abjNrXQcMRQlm0O4iyUSZ","Digital Veil","album","spotify:artist:2SDGIFzEh9xmE5zDKcMRkj","The Human Abstract","2011-03-08","day","8","2025-11-24T23:54:26Z"\n`
],
options: { type: 'text/csv;charset=utf-8' }
},
'saved_albums.csv',
{ "autoBom": false }
)
})