@@ -3,115 +3,71 @@ import { test, expect } from "@playwright/test";
33/**
44 * Shortlist tests.
55 *
6- * After clicking a heart button the aria-label changes from
7- * "Add X to shortlist" to "Remove X from shortlist".
8- * We look for the Remove label to confirm the save worked rather than
9- * checking aria-pressed on the original locator (which no longer matches
10- * after the label changes).
6+ * After clicking a heart the aria-label changes from "Add X to shortlist"
7+ * to "Remove X from shortlist". We confirm the save by looking for the
8+ * Remove-labelled button rather than checking aria-pressed on a locator
9+ * that no longer matches after the label changes.
1110 */
1211test . describe ( "Shortlist" , ( ) => {
1312 test . beforeEach ( async ( { page } ) => {
1413 await page . goto ( "http://localhost:3000" ) ;
15- await expect ( page . locator ( "text=Meadowside Nursery" ) ) . toBeVisible ( {
16- timeout : 10000 ,
17- } ) ;
14+ await expect ( page . locator ( "text=Meadowside Nursery" ) ) . toBeVisible ( { timeout : 10000 } ) ;
1815 } ) ;
1916
2017 test ( "save and unsave a nursery from a card" , async ( { page } ) => {
21- // Click the first heart
22- const addButton = page
23- . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } )
24- . first ( ) ;
25- await addButton . click ( ) ;
18+ // Save the first nursery
19+ await page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) . first ( ) . click ( ) ;
2620
27- // After saving the label changes to "Remove" — look for that
28- const removeButton = page
29- . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } )
30- . first ( ) ;
21+ // The label changes to Remove after saving — confirm that
22+ const removeButton = page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( ) ;
3123 await expect ( removeButton ) . toBeVisible ( { timeout : 5000 } ) ;
3224 await expect ( removeButton ) . toHaveAttribute ( "aria-pressed" , "true" ) ;
3325
3426 // Nav counter should appear
35- await expect (
36- page . getByRole ( "link" , { name : / V i e w s h o r t l i s t / } )
37- ) . toBeVisible ( { timeout : 5000 } ) ;
27+ await expect ( page . getByRole ( "link" , { name : / V i e w s h o r t l i s t / } ) ) . toBeVisible ( { timeout : 5000 } ) ;
3828
3929 // Unsave it
4030 await removeButton . click ( ) ;
41- // Button should flip back to "Add"
42- await expect (
43- page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) . first ( )
44- ) . toBeVisible ( { timeout : 5000 } ) ;
31+ await expect ( page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) . first ( ) ) . toBeVisible ( { timeout : 5000 } ) ;
4532 } ) ;
4633
4734 test ( "shortlist page shows saved nurseries and compare view" , async ( { page } ) => {
48- // Save first two nurseries
49- const hearts = page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) ;
50- await hearts . nth ( 0 ) . click ( ) ;
51- // Wait for first to be saved before clicking second
52- await expect (
53- page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( )
54- ) . toBeVisible ( { timeout : 5000 } ) ;
35+ // Save two nurseries
36+ const allAddButtons = page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) ;
5537
56- await hearts . first ( ) . click ( ) ; // re-click Add on the next un-saved card
57- // Actually nth(0) is now Remove, so click the new first Add
58- const secondAdd = page
59- . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } )
60- . first ( ) ;
61- await secondAdd . click ( ) ;
62- await expect (
63- page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } )
64- ) . toHaveCount ( 2 , { timeout : 5000 } ) ;
38+ await allAddButtons . first ( ) . click ( ) ;
39+ await expect ( page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) ) . toHaveCount ( 1 , { timeout : 5000 } ) ;
40+
41+ await allAddButtons . first ( ) . click ( ) ;
42+ await expect ( page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) ) . toHaveCount ( 2 , { timeout : 5000 } ) ;
6543
6644 // Navigate to shortlist
6745 await page . goto ( "http://localhost:3000/shortlist" ) ;
68- await expect (
69- page . getByRole ( "heading" , { name : "Your shortlist" } )
70- ) . toBeVisible ( { timeout : 10000 } ) ;
46+ await expect ( page . getByRole ( "heading" , { name : "Your shortlist" } ) ) . toBeVisible ( { timeout : 10000 } ) ;
7147
72- // Should see the compare tab ( only shown when 2+ nurseries saved)
48+ // Compare tab only shows when 2+ nurseries are saved
7349 const compareTab = page . getByRole ( "tab" , { name : "Compare" } ) ;
7450 await expect ( compareTab ) . toBeVisible ( { timeout : 5000 } ) ;
7551 await compareTab . click ( ) ;
7652
77- // Compare table row labels
78- await expect (
79- page . getByRole ( "rowheader" , { name : "Daily fee" } )
80- ) . toBeVisible ( ) ;
81- await expect (
82- page . getByRole ( "rowheader" , { name : "Ofsted" } )
83- ) . toBeVisible ( ) ;
53+ await expect ( page . getByRole ( "rowheader" , { name : "Daily fee" } ) ) . toBeVisible ( ) ;
54+ await expect ( page . getByRole ( "rowheader" , { name : "Ofsted" } ) ) . toBeVisible ( ) ;
8455 } ) ;
8556
8657 test ( "shortlist persists across page reloads" , async ( { page } ) => {
87- const addButton = page
88- . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } )
89- . first ( ) ;
90- await addButton . click ( ) ;
91-
92- // Confirm saved
93- await expect (
94- page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( )
95- ) . toBeVisible ( { timeout : 5000 } ) ;
58+ await page . getByRole ( "button" , { name : / A d d .* t o s h o r t l i s t / } ) . first ( ) . click ( ) ;
59+ await expect ( page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( ) ) . toBeVisible ( { timeout : 5000 } ) ;
9660
9761 await page . reload ( ) ;
98- await expect ( page . locator ( "text=Meadowside Nursery" ) ) . toBeVisible ( {
99- timeout : 10000 ,
100- } ) ;
62+ await expect ( page . locator ( "text=Meadowside Nursery" ) ) . toBeVisible ( { timeout : 10000 } ) ;
10163
102- // After reload the saved nursery should still show Remove
103- await expect (
104- page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( )
105- ) . toBeVisible ( { timeout : 5000 } ) ;
64+ // Should still show as saved after reload
65+ await expect ( page . getByRole ( "button" , { name : / R e m o v e .* f r o m s h o r t l i s t / } ) . first ( ) ) . toBeVisible ( { timeout : 5000 } ) ;
10666 } ) ;
10767
10868 test ( "empty shortlist shows guidance" , async ( { page } ) => {
10969 await page . goto ( "http://localhost:3000/shortlist" ) ;
110- await expect ( page . getByText ( "Nothing saved yet" ) ) . toBeVisible ( {
111- timeout : 10000 ,
112- } ) ;
113- await expect (
114- page . getByRole ( "link" , { name : "Browse nurseries" } )
115- ) . toBeVisible ( ) ;
70+ await expect ( page . getByText ( "Nothing saved yet" ) ) . toBeVisible ( { timeout : 10000 } ) ;
71+ await expect ( page . getByRole ( "link" , { name : "Browse nurseries" } ) ) . toBeVisible ( ) ;
11672 } ) ;
11773} ) ;
0 commit comments