@@ -26,26 +26,28 @@ describe('Course Registration', () => {
2626 // Navigate to the quilts page
2727 cy . visit ( '/quilts' ) ;
2828
29- // Get the first available course and store its name
30- cy . get ( '[data-cy="available-course-card"]' )
31- . first ( )
32- . find ( '[data-cy="course-title"]' )
33- . invoke ( 'text' )
34- . then ( ( text ) => {
35- let courseName = text . trim ( ) ;
36-
37- // Now click the register button
38- cy . get ( '[data-cy="available-course-card"]' )
39- . first ( )
40- . find ( '[data-cy="register-course-button"]' )
41- . click ( ) ;
42-
43- // Wait a moment for registration to process
44- cy . wait ( 1000 ) ;
45-
46- // Verify the course appears in the user's registered courses
47- cy . get ( '[data-cy="registered-course"]' ) . should ( 'contain' , courseName ) ;
48- } ) ;
29+ // Get the initial count of registered courses (handling the case when there are none)
30+ cy . get ( 'body' ) . then ( ( $body ) => {
31+ const initialCount = $body . find ( '[data-cy="registered-course"]' ) . length ;
32+
33+ // Now click the register button on the first available course
34+ cy . get ( '[data-cy="available-course-card"]' )
35+ . first ( )
36+ . find ( '[data-cy="register-course-button"]' )
37+ . click ( ) ;
38+
39+ // Wait a moment for registration to process
40+ cy . wait ( 1000 ) ;
41+
42+ // Verify the count of registered courses has increased
43+ if ( initialCount === 0 ) {
44+ // If there were no courses initially, we should have exactly one now
45+ cy . get ( '[data-cy="registered-course"]' ) . should ( 'have.length' , 1 ) ;
46+ } else {
47+ // Otherwise the count should have increased by one
48+ cy . get ( '[data-cy="registered-course"]' ) . should ( 'have.length' , initialCount + 1 ) ;
49+ }
50+ } ) ;
4951 } ) ;
5052
5153 it ( 'should allow registration using the custom command' , ( ) => {
0 commit comments