-
Notifications
You must be signed in to change notification settings - Fork 174
/
v-audio-track.spec.js
169 lines (142 loc) · 5.48 KB
/
v-audio-track.spec.js
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
import { fireEvent } from "@testing-library/vue"
import { createApp } from "vue"
import { render } from "~~/test/unit/test-utils/render"
import { i18n } from "~~/test/unit/test-utils/i18n"
import { getAudioObj } from "~~/test/unit/fixtures/audio"
import { useActiveMediaStore } from "~/stores/active-media"
import VAudioTrack from "~/components/VAudioTrack/VAudioTrack.vue"
window.HTMLMediaElement.prototype.play = () => {
/* mock */
}
const RouterLinkStub = createApp({}).component("RouterLink", {
template: "<a :href='href'><slot /></a>",
props: ["to"],
computed: {
href() {
return this.to
},
},
})._context.components.RouterLink
const stubs = {
VLicense: true,
VWaveform: true,
VAudioThumbnail: true,
RouterLink: RouterLinkStub,
}
describe("AudioTrack", () => {
let options = null
let props = null
let captureExceptionMock = vi.fn()
beforeEach(() => {
props = {
audio: getAudioObj(),
layout: "full",
}
const activeMediaStore = useActiveMediaStore()
activeMediaStore.$patch({
state: {
type: "audio",
id: "e19345b8-6937-49f7-a0fd-03bf057efc28",
message: null,
state: "paused",
},
})
options = {
props: props,
global: {
plugins: [i18n],
stubs,
},
}
})
it("should render the full audio track component even without duration", async () => {
options.props.layout = "full"
const { getByText } = await render(VAudioTrack, options)
const creator = getByText(props.audio.creator)
expect(creator).toBeInstanceOf(HTMLAnchorElement)
})
it("should show audio title as main page title in full layout", async () => {
options.props.layout = "full"
const { getByText } = await render(VAudioTrack, options)
// Title text appears multiple times in the track, so need to specify selector
const element = getByText(props.audio.title, { selector: "H1" })
expect(element).toBeInTheDocument()
})
it("should show audio creator in a full layout with link", async () => {
options.props.layout = "full"
const { getByText } = await render(VAudioTrack, options)
const element = getByText(props.audio.creator)
expect(element).toBeInstanceOf(HTMLAnchorElement)
expect(element).toHaveAttribute("href", props.audio.creator_url)
})
it("should render the row audio track component even without duration", async () => {
options.props.layout = "row"
const { getByText } = await render(VAudioTrack, options)
const creator = getByText("by " + props.audio.creator)
expect(creator).toBeTruthy()
})
// https://github.com/wordpress/openverse/issues/411
it.skip.each`
errorType | errorText
${"NotAllowedError"} | ${/Reproduction not allowed./i}
${"NotSupportedError"} | ${/This audio format is not supported by your browser./i}
${"AbortError"} | ${/You aborted playback./i}
${"UnknownError"} | ${/An unexpected error has occurred./i}
`(
"on play error displays a message instead of the waveform",
async ({ errorType, errorText }) => {
options.props.audio.url = "bad.url"
options.props.layout = "row"
options.global.stubs.VWaveform = false
options.global.stubs.VAudioThumbnail = true
vi.clearAllMocks()
const pauseStub = vi
.spyOn(window.HTMLMediaElement.prototype, "pause")
.mockImplementation(() => undefined)
const playError = new DOMException("msg", errorType)
const playStub = vi
.spyOn(window.HTMLMediaElement.prototype, "play")
.mockImplementation(() => Promise.reject(playError))
const { getByRole, getByText } = await render(VAudioTrack, options)
await fireEvent.click(getByRole("button"))
expect(playStub).toHaveBeenCalledTimes(1)
expect(pauseStub).toHaveBeenCalledTimes(1)
expect(getByText(errorText)).toBeVisible()
// Only the UnknownError should be sent to Sentry.
if (errorType === "UnknownError") {
// eslint-disable-next-line vitest/no-conditional-expect
expect(captureExceptionMock).toHaveBeenCalledWith(playError)
} else {
// eslint-disable-next-line vitest/no-conditional-expect
expect(captureExceptionMock).not.toHaveBeenCalled()
}
}
)
it("has blurred title in box layout when audio is sensitive", async () => {
options.props.audio.isSensitive = true
options.props.layout = "box"
options.props.size = "large"
const { getByText } = await render(VAudioTrack, options)
const h2 = getByText("This audio track may contain sensitive content.")
expect(h2).toHaveClass("blur-text")
})
it("has blurred info in row layout when audio is sensitive", async () => {
options.props.audio.isSensitive = true
options.props.layout = "row"
const { getByText } = await render(VAudioTrack, options)
const h2 = getByText("This audio track may contain sensitive content.")
expect(h2).toHaveClass("blur-text")
const creator = getByText("by Creator")
expect(creator).toHaveClass("blur-text")
})
it("is does not contain title or creator anywhere when the audio is sensitive", async () => {
options.props.audio.isSensitive = true
options.props.layout = "row"
const screen = await render(VAudioTrack, options)
let { title, creator } = options.props.audio
let match = RegExp(`(${title}|${creator})`)
expect(screen.queryAllByText(match)).toEqual([])
expect(screen.queryAllByTitle(match)).toEqual([])
expect(screen.queryAllByAltText(match)).toEqual([])
})
})