|
| 1 | +import React from "react" |
| 2 | +import { render, screen, waitFor, fireEvent } from "@testing-library/react" |
| 3 | +import renderer from "react-test-renderer" |
| 4 | +import { setupServer } from "msw/node" |
| 5 | +import FileSaver from "file-saver" |
| 6 | +import JSZip from "jszip" |
| 7 | + |
| 8 | +import PlaylistTable from "./PlaylistTable" |
| 9 | + |
| 10 | +import "../icons" |
| 11 | +import { handlers } from '../mocks/handlers_success' |
| 12 | + |
| 13 | +const server = setupServer(...handlers) |
| 14 | + |
| 15 | +server.listen({ |
| 16 | + onUnhandledRequest: 'warn' |
| 17 | +}) |
| 18 | + |
| 19 | +beforeAll(() => { |
| 20 | + global.Blob = function (content, options) { return ({content, options}) } |
| 21 | +}) |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + jest.restoreAllMocks() |
| 25 | +}) |
| 26 | + |
| 27 | +// Use a snapshot test to ensure exact component rendering |
| 28 | +test("playlist loading", async () => { |
| 29 | + const component = renderer.create(<PlaylistTable />) |
| 30 | + const instance = component.getInstance() |
| 31 | + |
| 32 | + await waitFor(() => { |
| 33 | + expect(instance.state.playlistCount).toEqual(1) |
| 34 | + }) |
| 35 | + |
| 36 | + expect(component.toJSON()).toMatchSnapshot(); |
| 37 | +}) |
| 38 | + |
| 39 | +test("single playlist exporting", async () => { |
| 40 | + const saveAsMock = jest.spyOn(FileSaver, "saveAs") |
| 41 | + saveAsMock.mockImplementation(jest.fn()) |
| 42 | + |
| 43 | + render(<PlaylistTable />); |
| 44 | + |
| 45 | + await waitFor(() => { |
| 46 | + expect(screen.getByText(/Export All/)).toBeInTheDocument() |
| 47 | + }) |
| 48 | + |
| 49 | + const linkElement = screen.getAllByText("Export")[0] |
| 50 | + |
| 51 | + expect(linkElement).toBeInTheDocument() |
| 52 | + |
| 53 | + fireEvent.click(linkElement) |
| 54 | + |
| 55 | + await waitFor(() => { |
| 56 | + expect(saveAsMock).toHaveBeenCalledTimes(1) |
| 57 | + expect(saveAsMock).toHaveBeenCalledWith( |
| 58 | + { |
| 59 | + content: [ |
| 60 | + '"Track URI","Track Name","Artist URI","Artist Name","Album URI","Album Name","Disc Number","Track Number","Track Duration (ms)","Added By","Added At"\n' + |
| 61 | + '"spotify:track:1GrLfs4TEvAZ86HVzXHchS","Crying","spotify:artist:4TXdHyuAOl3rAOFmZ6MeKz","Six by Seven","spotify:album:4iwv7b8gDPKztLkKCbWyhi","Best of Six By Seven","1","3","198093","","2020-07-19T09:24:39Z"\n' |
| 62 | + ], |
| 63 | + options: { type: 'text/csv;charset=utf-8' } |
| 64 | + }, |
| 65 | + 'liked.csv', |
| 66 | + true |
| 67 | + ) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +test("exporting of all playlist", async () => { |
| 72 | + const saveAsMock = jest.spyOn(FileSaver, "saveAs") |
| 73 | + saveAsMock.mockImplementation(jest.fn()) |
| 74 | + |
| 75 | + const jsZipFileMock = jest.spyOn(JSZip.prototype, 'file') |
| 76 | + const jsZipGenerateAsync = jest.spyOn(JSZip.prototype, 'generateAsync') |
| 77 | + jsZipGenerateAsync.mockResolvedValue("zip_content") |
| 78 | + |
| 79 | + render(<PlaylistTable />); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(screen.getByText(/Export All/)).toBeInTheDocument() |
| 83 | + }) |
| 84 | + |
| 85 | + const linkElement = screen.getByText("Export All") |
| 86 | + |
| 87 | + expect(linkElement).toBeInTheDocument() |
| 88 | + |
| 89 | + fireEvent.click(linkElement) |
| 90 | + |
| 91 | + await waitFor(() => { |
| 92 | + expect(jsZipFileMock).toHaveBeenCalledTimes(2) |
| 93 | + expect(jsZipFileMock).toHaveBeenCalledWith( |
| 94 | + "liked.csv", |
| 95 | + '"Track URI","Track Name","Artist URI","Artist Name","Album URI","Album Name","Disc Number","Track Number","Track Duration (ms)","Added By","Added At"\n' + |
| 96 | + '"spotify:track:1GrLfs4TEvAZ86HVzXHchS","Crying","spotify:artist:4TXdHyuAOl3rAOFmZ6MeKz","Six by Seven","spotify:album:4iwv7b8gDPKztLkKCbWyhi","Best of Six By Seven","1","3","198093","","2020-07-19T09:24:39Z"\n' |
| 97 | + ) |
| 98 | + expect(jsZipFileMock).toHaveBeenCalledWith( |
| 99 | + "ghostpoet_–_peanut_butter_blues_and_melancholy_jam.csv", |
| 100 | + '"Track URI","Track Name","Artist URI","Artist Name","Album URI","Album Name","Disc Number","Track Number","Track Duration (ms)\",\"Added By\",\"Added At\"\n' + |
| 101 | + '"spotify:track:7ATyvp3TmYBmGW7YuC8DJ3","One Twos / Run Run Run","spotify:artist:69lEbRQRe29JdyLrewNAvD","Ghostpoet","spotify:album:6jiLkuSnhzDvzsHJlweoGh","Peanut Butter Blues and Melancholy Jam","1","1","241346","spotify:user:watsonbox","2020-11-03T15:19:04Z"\n' + |
| 102 | + '"spotify:track:0FNanBLvmFEDyD75Whjj52","Us Against Whatever Ever","spotify:artist:69lEbRQRe29JdyLrewNAvD","Ghostpoet","spotify:album:6jiLkuSnhzDvzsHJlweoGh","Peanut Butter Blues and Melancholy Jam","1","2","269346","spotify:user:watsonbox","2020-11-03T15:19:04Z"\n' |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + await waitFor(() => { |
| 107 | + expect(saveAsMock).toHaveBeenCalledTimes(1) |
| 108 | + expect(saveAsMock).toHaveBeenCalledWith("zip_content", "spotify_playlists.zip") |
| 109 | + }) |
| 110 | +}) |
0 commit comments