-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathe2e.spec.ts
238 lines (194 loc) · 8.73 KB
/
e2e.spec.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type { Page } from '@playwright/test'
import { expect, test } from '@playwright/test'
import path from 'path'
import { wait } from 'payload/shared'
import { fileURLToPath } from 'url'
import type { PayloadTestSDK } from '../../../helpers/sdk/index.js'
import type { Config } from '../../payload-types.js'
import {
ensureCompilationIsDone,
initPageConsoleErrorCatch,
openDocDrawer,
saveDocAndAssert,
} from '../../../helpers.js'
import { AdminUrlUtil } from '../../../helpers/adminUrlUtil.js'
import { initPayloadE2ENoConfig } from '../../../helpers/initPayloadE2ENoConfig.js'
import { reInitializeDB } from '../../../helpers/reInitializeDB.js'
import { RESTClient } from '../../../helpers/rest.js'
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../../../playwright.config.js'
import { uploadsSlug } from '../../slugs.js'
const filename = fileURLToPath(import.meta.url)
const currentFolder = path.dirname(filename)
const dirname = path.resolve(currentFolder, '../../')
const { beforeAll, beforeEach, describe } = test
let payload: PayloadTestSDK<Config>
let client: RESTClient
let page: Page
let serverURL: string
// If we want to make this run in parallel: test.describe.configure({ mode: 'parallel' })
let url: AdminUrlUtil
describe('Upload', () => {
beforeAll(async ({ browser }, testInfo) => {
testInfo.setTimeout(TEST_TIMEOUT_LONG)
process.env.SEED_IN_CONFIG_ONINIT = 'false' // Makes it so the payload config onInit seed is not run. Otherwise, the seed would be run unnecessarily twice for the initial test run - once for beforeEach and once for onInit
;({ payload, serverURL } = await initPayloadE2ENoConfig({
dirname,
// prebuild,
}))
url = new AdminUrlUtil(serverURL, uploadsSlug)
const context = await browser.newContext()
page = await context.newPage()
initPageConsoleErrorCatch(page)
await ensureCompilationIsDone({ page, serverURL })
})
beforeEach(async () => {
await reInitializeDB({
serverURL,
snapshotKey: 'fieldsTest',
uploadsDir: path.resolve(dirname, './collections/Upload/uploads'),
})
if (client) {
await client.logout()
}
client = new RESTClient(null, { defaultSlug: 'users', serverURL })
await client.login()
await ensureCompilationIsDone({ page, serverURL })
})
async function uploadImage() {
await page.goto(url.create)
// create a jpg upload
await page
.locator('.file-field__upload input[type="file"]')
.setInputFiles(path.resolve(dirname, './collections/Upload/payload.jpg'))
await expect(page.locator('.file-field .file-field__filename')).toHaveValue('payload.jpg')
await saveDocAndAssert(page)
}
test('should upload files', async () => {
await uploadImage()
})
test('should upload files from remote URL', async () => {
await uploadImage()
await page.goto(url.create)
const pasteURLButton = page.locator('.file-field__upload button', {
hasText: 'Paste URL',
})
await pasteURLButton.click()
const remoteImage = 'https://payloadcms.com/images/og-image.jpg'
const inputField = page.locator('.file-field__upload .file-field__remote-file')
await inputField.fill(remoteImage)
const addFileButton = page.locator('.file-field__add-file')
await addFileButton.click()
await expect(page.locator('.file-field .file-field__filename')).toHaveValue('og-image.jpg')
await saveDocAndAssert(page)
await expect(page.locator('.file-field .file-details img')).toHaveAttribute(
'src',
/^data:image\/png;base64,/,
)
})
// test that the image renders
test('should render uploaded image', async () => {
await uploadImage()
await expect(page.locator('.file-field .file-details img')).toHaveAttribute(
'src',
/^data:image\/png;base64,/,
)
})
test('should upload using the document drawer', async () => {
await uploadImage()
await wait(1000)
// Open the media drawer and create a png upload
await openDocDrawer(page, '#field-media .upload__createNewToggler')
await page
.locator('[id^=doc-drawer_uploads_1_] .file-field__upload input[type="file"]')
.setInputFiles(path.resolve(dirname, './uploads/payload.png'))
await expect(
page.locator('[id^=doc-drawer_uploads_1_] .file-field__upload .file-field__filename'),
).toHaveValue('payload.png')
await page.locator('[id^=doc-drawer_uploads_1_] #action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
// Assert that the media field has the png upload
await expect(
page.locator('.field-type.upload .upload-relationship-details__filename a'),
).toHaveAttribute('href', '/api/uploads/file/payload-1.png')
await expect(
page.locator('.field-type.upload .upload-relationship-details__filename a'),
).toContainText('payload-1.png')
await expect(
page.locator('.field-type.upload .upload-relationship-details img'),
).toHaveAttribute('src', /^data:image\/png;base64,/)
await saveDocAndAssert(page)
})
test.skip('should upload after editing image inside a document drawer', async () => {
await uploadImage()
await wait(1000)
// Open the media drawer and create a png upload
await openDocDrawer(page, '#field-media .upload__createNewToggler')
await page
.locator('[id^=doc-drawer_uploads_1_] .file-field__upload input[type="file"]')
.setInputFiles(path.resolve(dirname, './uploads/payload.png'))
await expect(
page.locator('[id^=doc-drawer_uploads_1_] .file-field__upload .file-field__filename'),
).toHaveValue('payload.png')
await page.locator('[id^=doc-drawer_uploads_1_] .file-field__edit').click()
await page
.locator('[id^=edit-upload] .edit-upload__input input[name="Width (px)"]')
.nth(1)
.fill('200')
await page
.locator('[id^=edit-upload] .edit-upload__input input[name="Height (px)"]')
.nth(1)
.fill('200')
await page.locator('[id^=edit-upload] button:has-text("Apply Changes")').nth(1).click()
await page.locator('[id^=doc-drawer_uploads_1_] #action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
// Assert that the media field has the png upload
await expect(
page.locator('.field-type.upload .upload-relationship-details__filename a'),
).toHaveAttribute('href', '/api/uploads/file/payload-1.png')
await expect(
page.locator('.field-type.upload .upload-relationship-details__filename a'),
).toContainText('payload-1.png')
await expect(
page.locator('.field-type.upload .upload-relationship-details img'),
).toHaveAttribute('src', '/api/uploads/file/payload-1.png')
await saveDocAndAssert(page)
})
test('should clear selected upload', async () => {
await uploadImage()
await wait(1000) // TODO: Fix this. Need to wait a bit until the form in the drawer mounted, otherwise values sometimes disappear. This is an issue for all drawers
await openDocDrawer(page, '#field-media .upload__createNewToggler')
await wait(1000)
await page
.locator('[id^=doc-drawer_uploads_1_] .file-field__upload input[type="file"]')
.setInputFiles(path.resolve(dirname, './uploads/payload.png'))
await expect(
page.locator('[id^=doc-drawer_uploads_1_] .file-field__upload .file-field__filename'),
).toHaveValue('payload.png')
await page.locator('[id^=doc-drawer_uploads_1_] #action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await page.locator('.field-type.upload .upload-relationship-details__remove').click()
})
test('should select using the list drawer and restrict mimetype based on filterOptions', async () => {
await uploadImage()
await openDocDrawer(page, '.field-type.upload .upload__listToggler')
const jpgImages = page.locator('[id^=list-drawer_1_] .upload-gallery img[src$=".jpg"]')
await expect
.poll(async () => await jpgImages.count(), { timeout: POLL_TOPASS_TIMEOUT })
.toEqual(0)
})
test.skip('should show drawer for input field when enableRichText is false', async () => {
const uploads3URL = new AdminUrlUtil(serverURL, 'uploads3')
await page.goto(uploads3URL.create)
// create file in uploads 3 collection
await page
.locator('.file-field__upload input[type="file"]')
.setInputFiles(path.resolve(dirname, './collections/Upload/payload.jpg'))
await expect(page.locator('.file-field .file-field__filename')).toContainText('payload.jpg')
await page.locator('#action-save').click()
await wait(200)
// open drawer
await openDocDrawer(page, '.field-type.upload .list-drawer__toggler')
// check title
await expect(page.locator('.list-drawer__header-text')).toContainText('Uploads 3')
})
})