|
| 1 | +import { expect } from "@playwright/test"; |
| 2 | +import type { Recipe } from "@prisma/client"; |
| 3 | +import { test as baseTest } from "./fixtures.js"; |
| 4 | + |
| 5 | +type Fixtures = { |
| 6 | + someFilterableRecipes: { recipe1: Recipe; recipe2: Recipe; sandwich: Recipe }; |
| 7 | +}; |
| 8 | +const test = baseTest.extend<Fixtures>({ |
| 9 | + someFilterableRecipes: async ( |
| 10 | + { testCategory, testUser, testRecipe }, |
| 11 | + use |
| 12 | + ) => { |
| 13 | + const dessert = await testCategory.create("dessert"); |
| 14 | + const supper = await testCategory.create("supper"); |
| 15 | + const lunch = await testCategory.create("lunch"); |
| 16 | + const user = await testUser.create({ |
| 17 | + username: "testuser", |
| 18 | + name: "User Name", |
| 19 | + }); |
| 20 | + const cookuser = await testUser.create({ |
| 21 | + username: "cook", |
| 22 | + name: "Cook User", |
| 23 | + }); |
| 24 | + const [recipe1, recipe2, sandwich] = await Promise.all([ |
| 25 | + testRecipe.create({ |
| 26 | + author: { connect: { id: user.id } }, |
| 27 | + name: "Test Recipe 1", |
| 28 | + slug: "test-recipe", |
| 29 | + ingredients: { |
| 30 | + create: [ |
| 31 | + { order: 0, name: "flour" }, |
| 32 | + { order: 1, name: "sugar" }, |
| 33 | + { order: 2, name: "eggs" }, |
| 34 | + { order: 3, name: "butter" }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + categories: { connect: { id: dessert.id } }, |
| 38 | + }), |
| 39 | + testRecipe.create({ |
| 40 | + author: { connect: { id: user.id } }, |
| 41 | + name: "Recipe 2", |
| 42 | + slug: "test-recipe-2", |
| 43 | + ingredients: { |
| 44 | + create: [{ order: 0, name: "bread" }], |
| 45 | + }, |
| 46 | + categories: { connect: { id: supper.id } }, |
| 47 | + cookingHistory: { |
| 48 | + create: [ |
| 49 | + { |
| 50 | + cookedAtYear: 2023, |
| 51 | + cookedAtMonth: 4, |
| 52 | + cook: { connect: { id: user.id } }, |
| 53 | + }, |
| 54 | + { |
| 55 | + cookedAtYear: 2023, |
| 56 | + cookedAtMonth: 10, |
| 57 | + cook: { connect: { id: cookuser.id } }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }), |
| 62 | + testRecipe.create({ |
| 63 | + author: { connect: { id: user.id } }, |
| 64 | + name: "Sandwich", |
| 65 | + slug: "sandwich", |
| 66 | + ingredients: { |
| 67 | + create: [ |
| 68 | + { order: 0, name: "bread" }, |
| 69 | + { order: 1, name: "pickles" }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + categories: { connect: { id: lunch.id } }, |
| 73 | + }), |
| 74 | + ]); |
| 75 | + await use({ recipe1, recipe2, sandwich }); |
| 76 | + }, |
| 77 | +}); |
| 78 | + |
| 79 | +test("Category Filters", async ({ page, someFilterableRecipes }) => { |
| 80 | + const { recipe2 } = someFilterableRecipes; |
| 81 | + |
| 82 | + await page.goto("/categories"); |
| 83 | + |
| 84 | + await expect(page.getByLabel(/Filter/)).toHaveValue(""); |
| 85 | + |
| 86 | + await page.getByLabel(/Filter/).fill("s"); |
| 87 | + await expect(page).toHaveURL("/categories?filter=s"); |
| 88 | + |
| 89 | + await expect |
| 90 | + .soft(page.locator("#categories").getByRole("listitem").locator("summary")) |
| 91 | + .toHaveText([/^Supper$/i, /^Dessert$/i]); |
| 92 | + |
| 93 | + await page |
| 94 | + .locator("#categories") |
| 95 | + .getByRole("listitem") |
| 96 | + .filter({ hasText: /Supper/i }) |
| 97 | + .click(); |
| 98 | + await expect |
| 99 | + .soft( |
| 100 | + page |
| 101 | + .locator("#categories") |
| 102 | + .getByRole("listitem") |
| 103 | + .filter({ hasText: /Supper/i }) |
| 104 | + .getByRole("listitem") |
| 105 | + ) |
| 106 | + .toHaveText([recipe2.name]); |
| 107 | +}); |
| 108 | + |
| 109 | +test("Categories sort by history when logged in", async ({ |
| 110 | + page, |
| 111 | + someFilterableRecipes, |
| 112 | + testLogin, |
| 113 | + testRecipe, |
| 114 | +}) => { |
| 115 | + const { recipe2: supperApril } = someFilterableRecipes; |
| 116 | + const { userId: _ } = testLogin; |
| 117 | + |
| 118 | + const [supperMay, supperNotCooked] = await Promise.all([ |
| 119 | + testRecipe.create({ |
| 120 | + author: { connect: { username: "testuser" } }, |
| 121 | + name: "Cooked in May", |
| 122 | + slug: "cooked-in-may", |
| 123 | + ingredients: { |
| 124 | + create: [{ order: 0, name: "bread" }], |
| 125 | + }, |
| 126 | + categories: { connect: { name: "supper" } }, |
| 127 | + cookingHistory: { |
| 128 | + create: [ |
| 129 | + { |
| 130 | + cookedAtYear: 2023, |
| 131 | + cookedAtMonth: 5, |
| 132 | + cook: { connect: { username: "testuser" } }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + }), |
| 137 | + testRecipe.create({ |
| 138 | + author: { connect: { username: "testuser" } }, |
| 139 | + name: "Not Cooked", |
| 140 | + slug: "not-cooked", |
| 141 | + ingredients: { |
| 142 | + create: [{ order: 0, name: "bread" }], |
| 143 | + }, |
| 144 | + categories: { connect: { name: "supper" } }, |
| 145 | + }), |
| 146 | + ]); |
| 147 | + |
| 148 | + await page.goto("/categories?filter=supper"); |
| 149 | + |
| 150 | + await expect |
| 151 | + .soft(page.locator("#categories").getByRole("listitem").locator("summary")) |
| 152 | + .toHaveText([/^Supper$/i]); |
| 153 | + |
| 154 | + await expect |
| 155 | + .soft( |
| 156 | + page |
| 157 | + .locator("#categories") |
| 158 | + .getByRole("listitem") |
| 159 | + .filter({ hasText: /Supper/i }) |
| 160 | + .getByRole("listitem") |
| 161 | + ) |
| 162 | + .toHaveText([ |
| 163 | + supperNotCooked.name, |
| 164 | + `${supperApril.name} last cooked Apr 2023`, |
| 165 | + `${supperMay.name} last cooked May 2023`, |
| 166 | + ]); |
| 167 | +}); |
0 commit comments