[wp-trac] [WordPress Trac] #48978: Add a new test case for E2E test (Show admin bar if user logged in)

WordPress Trac noreply at wordpress.org
Fri Feb 14 07:15:49 UTC 2020


#48978: Add a new test case for E2E test (Show admin bar if user logged in)
------------------------------+------------------------------
 Reporter:  hideokamoto       |       Owner:  (none)
     Type:  enhancement       |      Status:  new
 Priority:  normal            |   Milestone:  Awaiting Review
Component:  Build/Test Tools  |     Version:
 Severity:  normal            |  Resolution:
 Keywords:  needs-patch       |     Focuses:  javascript
------------------------------+------------------------------

Comment (by hideokamoto):

 Thanks for the feedback!
 I've updated the patch today.

 > expect( nodes.length ).not.toEqual( 0 ); I know the test above has a
 similar expectation, but as far as I'm aware more than one admin bar would
 be unexpected. I think it'd be better to change this to expect(
 nodes.length ).toBe( 1 );, it's also safe to use the stricter toBe matcher
 for numbers.

 I've replaced the assertion from `expect( nodes.length ).not.toEqual( 0 )`
 to `expect( ! ! ( nodes ) ).toBe( true )`.

 Because,  the assertion `expect( nodes.length ).toBe( 1 )`  not works
 well.

 {{{
 > node ./tests/e2e/run-tests.js

  FAIL  tests/e2e/specs/hello.test.js
   Hello World
     ✓ Should load properly (1814ms)
   Admin Bar
     ✕ Should show admin bar when user logged in (1021ms)

   ● Admin Bar › Should show admin bar when user logged in

     TypeError: Cannot read property 'length' of null

       15 |              await page.goto( createURL( '/' ) );
       16 |              const nodes = await page.$( '#wpadminbar1' );
     > 17 |              expect( nodes.length ).toBe( 1 );
          |                            ^
       18 |      } );
       19 | } );

       at Object.<anonymous> (specs/hello.test.js:17:17)

 Test Suites: 1 failed, 1 total
 Tests:       1 failed, 1 passed, 2 total
 Snapshots:   0 total
 Time:        4.018s
 Ran all test suites.
 }}}

 So I change the code to check just the element exists.

 **Pass case**
 {{{
 describe( 'Admin Bar', () => {
         it( 'Should show admin bar when user logged in' , async () => {
                 await loginUser();
                 await page.goto( createURL( '/' ) );
                 const nodes = await page.$( '#wpadminbar' );
                 expect( ! ! ( nodes ) ).toBe( true );
         } );
 } );

   Admin Bar
     ✓ Should show admin bar when user logged in (1041ms)
 }}}

 ** Failure case **

 {{{
 describe( 'Admin Bar', () => {
         it( 'Should show admin bar when user logged in' , async () => {
                 await loginUser();
                 await page.goto( createURL( '/' ) );
                 const nodes = await page.$( '#wpadminbar111' );
                 expect( ! ! ( nodes ) ).toBe( true );
         } );
 } );

   Admin Bar
     ✕ Should show admin bar when user logged in (802ms)

   ● Admin Bar › Should show admin bar when user logged in

     expect(received).toBe(expected) // Object.is equality

     Expected: true
     Received: false

       15 |              await page.goto( createURL( '/' ) );
       16 |              const nodes = await page.$( '#wpadminbar111' );
     > 17 |              expect( ! ! ( nodes ) ).toBe( true );
          |                                      ^
       18 |      } );
       19 | } );

       at Object.<anonymous> (specs/hello.test.js:17:27)
 }}}

 > I think it could be worth also testing for absence of the admin bar when
 logged out, this would likely be a separate test case.

 I think so too.
 But currently, there is a no helper function to logout the dashboard.
 So probably, we need to create a helper function to logout the dashboard.

 //
 Kindly regards.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48978#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list