|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { deletePost, getPostIdInURL, createPostWithFeatureImage } from "./helpers/post"; |
| 5 | + |
| 6 | +test.describe('feature image', () => { |
| 7 | + let postIdsToDelete: string[] = []; |
| 8 | + |
| 9 | + test.afterAll(async ({ request }) => { |
| 10 | + // delete all posts created in the tests |
| 11 | + for (const postId of postIdsToDelete) { |
| 12 | + await deletePost(request, postId); |
| 13 | + } |
| 14 | + }) |
| 15 | + |
| 16 | + test('it should be possible to save without a feature image', async ({ page, request }) => { |
| 17 | + // Open a new post |
| 18 | + await page.goto('/posts'); |
| 19 | + await page.getByRole('button', { name: 'New post' }).click(); |
| 20 | + await page.waitForURL(/.*\/posts\/\d+/); |
| 21 | + // Store the post id to delete after the test |
| 22 | + const postId = getPostIdInURL(page); |
| 23 | + if (postId) { |
| 24 | + // Store the post id to delete after the test |
| 25 | + postIdsToDelete.push(postId); |
| 26 | + } |
| 27 | + |
| 28 | + // Wait for the editor to load |
| 29 | + await page.getByTestId('editor').waitFor(); |
| 30 | + |
| 31 | + // Save the new post without adding a feature image |
| 32 | + await page.keyboard.down('Control'); |
| 33 | + await page.keyboard.press('s'); |
| 34 | + |
| 35 | + // Check that the post was saved successfully |
| 36 | + const saveNotificationTitle = page.locator('#toast-1-title'); |
| 37 | + const saveNotificationDescription = page.locator('#toast-1-description'); |
| 38 | + expect(saveNotificationTitle).toBeVisible(); |
| 39 | + expect(await saveNotificationTitle.innerText()).toBe('Post has been updated.'); |
| 40 | + |
| 41 | + expect(saveNotificationDescription).toBeVisible(); |
| 42 | + expect(await saveNotificationDescription.innerText()).toBe('The post has been updated.'); |
| 43 | + }) |
| 44 | + |
| 45 | + test('it should be possible to save with a feature image', async ({ page, request }) => { |
| 46 | + // Open a new post |
| 47 | + await page.goto('/posts'); |
| 48 | + await page.getByRole('button', { name: 'New post' }).click(); |
| 49 | + await page.waitForURL(/.*\/posts\/\d+/); |
| 50 | + // Store the post id to delete after the test |
| 51 | + const postId = getPostIdInURL(page); // Extract the new post id from the URL |
| 52 | + if (postId) { |
| 53 | + // Store the post id to delete after the test |
| 54 | + postIdsToDelete.push(postId); |
| 55 | + } |
| 56 | + |
| 57 | + // Prepare promises before clicking the button |
| 58 | + const fileChooserPromise = page.waitForEvent('filechooser'); |
| 59 | + const waitForUploadPromise = page.waitForResponse('**/api/upload'); |
| 60 | + |
| 61 | + // Open the drawer |
| 62 | + const drawerButton = page.getByTestId('open-post-drawer'); |
| 63 | + await drawerButton.click(); |
| 64 | + |
| 65 | + // Select a feature image |
| 66 | + const fileChooserButton = page.locator('text="Select Image"'); |
| 67 | + await fileChooserButton.click(); |
| 68 | + const fileChooser = await fileChooserPromise; |
| 69 | + await fileChooser.setFiles(path.join(__dirname, '/fixtures/feature-image.png')); |
| 70 | + |
| 71 | + // Wait for feature image upload to complete before saving the post |
| 72 | + await waitForUploadPromise; |
| 73 | + |
| 74 | + // Prepare a promise before pressing the save shortcut |
| 75 | + const waitForSavePromise = page.waitForResponse(`**/api/posts/${postId}?populate=feature_image`); |
| 76 | + |
| 77 | + // Save the new post with a feature image |
| 78 | + await page.keyboard.down('Control'); |
| 79 | + await page.keyboard.press('s'); |
| 80 | + |
| 81 | + // Wait for the request to complete |
| 82 | + await waitForSavePromise; |
| 83 | + |
| 84 | + // Check that the post was saved successfully |
| 85 | + const saveNotificationTitle = page.locator('#toast-1-title'); |
| 86 | + expect(saveNotificationTitle).toBeVisible(); |
| 87 | + expect(await saveNotificationTitle.innerText()).toBe('Post has been updated.'); |
| 88 | + }) |
| 89 | + |
| 90 | + test('the saved image should be visible in the drawer and can be deleted', async ({ page, request }) => { |
| 91 | + // Prepare existing post that has a feature image |
| 92 | + const postId = await createPostWithFeatureImage(page, request); |
| 93 | + if (postId) { |
| 94 | + // Store the post id to delete after the test |
| 95 | + postIdsToDelete.push(postId); |
| 96 | + } |
| 97 | + |
| 98 | + // Open the post |
| 99 | + await page.goto(`/posts/${postId}`); |
| 100 | + |
| 101 | + // Check that saved feature image is visible in the drawer |
| 102 | + await page.getByTestId('open-post-drawer').click(); |
| 103 | + expect(page.getByTestId('feature-image')).toBeVisible(); |
| 104 | + |
| 105 | + // Check that it's possible to delete the feature image |
| 106 | + const deleteImageButton = page.getByTestId('delete-feature-image'); |
| 107 | + await deleteImageButton.click(); |
| 108 | + |
| 109 | + // Prepare a promise before pressing the save shortcut |
| 110 | + const nextPromise = page.waitForResponse(`**/api/posts/${postId}?populate=feature_image`); |
| 111 | + await page.keyboard.down('Control'); |
| 112 | + await page.keyboard.press('s'); |
| 113 | + |
| 114 | + // Wait for the request to complete |
| 115 | + await nextPromise; |
| 116 | + |
| 117 | + // Wait for the save notification to appear |
| 118 | + const saveNotificationTitle = page.locator('#toast-1-title'); |
| 119 | + expect(saveNotificationTitle).toBeVisible(); |
| 120 | + expect(await saveNotificationTitle.innerText()).toBe('Post has been updated.'); |
| 121 | + |
| 122 | + // Reopen the post |
| 123 | + await page.goto(`/posts/${postId}`); |
| 124 | + await page.getByTestId('open-post-drawer').click(); |
| 125 | + // Wait for the drawer to open |
| 126 | + await page.locator('text="Select Image"').waitFor(); |
| 127 | + |
| 128 | + // Check that deleted image has dissapeared |
| 129 | + expect(page.getByTestId('feature-image')).not.toBeVisible(); |
| 130 | + }); |
| 131 | +}) |
0 commit comments