-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathnote-images.test.ts
More file actions
153 lines (135 loc) · 4.68 KB
/
note-images.test.ts
File metadata and controls
153 lines (135 loc) · 4.68 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
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
import { faker } from '@faker-js/faker'
import { type NoteImage, type Note } from '@prisma/client'
import { prisma } from '#app/utils/db.server.ts'
import { expect, test } from '#tests/playwright-utils.ts'
test('Users can create note with an image', async ({
page,
navigate,
login,
}) => {
const user = await login()
await navigate('/users/:username/notes', { username: user.username })
const newNote = createNote()
const altText = 'cute koala'
await page.getByRole('link', { name: 'new note' }).click()
// fill in form and submit
await page.getByRole('textbox', { name: 'title' }).fill(newNote.title)
await page.getByRole('textbox', { name: 'content' }).fill(newNote.content)
await page
.getByLabel('image')
.nth(0)
.setInputFiles('tests/fixtures/images/kody-notes/cute-koala.png')
await page.getByRole('textbox', { name: 'alt text' }).fill(altText)
await page.getByRole('button', { name: 'submit' }).click()
await expect(page).toHaveURL(new RegExp(`/users/${user.username}/notes/.*`))
await expect(page.getByRole('heading', { name: newNote.title })).toBeVisible()
await expect(
page.getByRole('region', { name: newNote.title }).getByAltText(altText),
).toBeVisible()
})
test('Users can create note with multiple images', async ({
page,
navigate,
login,
}) => {
const user = await login()
await navigate('/users/:username/notes', { username: user.username })
const newNote = createNote()
const altText1 = 'cute koala'
const altText2 = 'koala coder'
await page.getByRole('link', { name: 'new note' }).click()
// fill in form and submit
await page.getByRole('textbox', { name: 'title' }).fill(newNote.title)
await page.getByRole('textbox', { name: 'content' }).fill(newNote.content)
await page
.getByLabel('image')
.nth(0)
.setInputFiles('tests/fixtures/images/kody-notes/cute-koala.png')
await page.getByLabel('alt text').nth(0).fill(altText1)
await page.getByRole('button', { name: 'add image' }).click()
await page
.getByLabel('image')
.nth(1)
.setInputFiles('tests/fixtures/images/kody-notes/koala-coder.png')
await page.getByLabel('alt text').nth(1).fill(altText2)
await page.getByRole('button', { name: 'submit' }).click()
await expect(page).toHaveURL(new RegExp(`/users/${user.username}/notes/.*`))
await expect(page.getByRole('heading', { name: newNote.title })).toBeVisible()
await expect(page.getByAltText(altText1)).toBeVisible()
await expect(page.getByAltText(altText2)).toBeVisible()
})
test('Users can edit note image', async ({ page, navigate, login }) => {
const user = await login()
const note = await prisma.note.create({
select: { id: true },
data: {
...createNoteWithImage(),
ownerId: user.id,
},
})
await navigate('/users/:username/notes/:noteId', {
username: user.username,
noteId: note.id,
})
// edit the image
await page.getByRole('link', { name: 'Edit', exact: true }).click()
const updatedImage = {
altText: 'koala coder',
location: 'tests/fixtures/images/kody-notes/koala-coder.png',
}
await page.getByLabel('image').nth(0).setInputFiles(updatedImage.location)
await page.getByLabel('alt text').nth(0).fill(updatedImage.altText)
await page.getByRole('button', { name: 'submit' }).click()
await expect(page).toHaveURL(`/users/${user.username}/notes/${note.id}`)
await expect(page.getByAltText(updatedImage.altText)).toBeVisible()
})
test('Users can delete note image', async ({ page, navigate, login }) => {
const user = await login()
const note = await prisma.note.create({
select: { id: true, title: true },
data: {
...createNoteWithImage(),
ownerId: user.id,
},
})
await navigate('/users/:username/notes/:noteId', {
username: user.username,
noteId: note.id,
})
await expect(page.getByRole('heading', { name: note.title })).toBeVisible()
const images = page
.getByRole('region', { name: note.title })
.getByRole('list')
.getByRole('listitem')
.getByRole('img')
await expect(images).toHaveCount(1)
await page.getByRole('link', { name: 'Edit', exact: true }).click()
await page.getByRole('button', { name: 'remove image' }).click()
await page.getByRole('button', { name: 'submit' }).click()
await expect(page).toHaveURL(`/users/${user.username}/notes/${note.id}`)
await expect(images).toHaveCount(0)
})
function createNote() {
return {
title: faker.lorem.words(3),
content: faker.lorem.paragraphs(3),
} satisfies Omit<Note, 'id' | 'createdAt' | 'updatedAt' | 'type' | 'ownerId'>
}
function createNoteWithImage() {
return {
...createNote(),
images: {
create: {
altText: 'cute koala',
objectKey: 'kody-notes/cute-koala.png',
},
},
} satisfies Omit<
Note,
'id' | 'createdAt' | 'updatedAt' | 'type' | 'ownerId'
> & {
images: {
create: Pick<NoteImage, 'altText' | 'objectKey'>
}
}
}