Skip to content

Commit

Permalink
E2E Framework: fix empty array check in RestAPIClient. (#74741)
Browse files Browse the repository at this point in the history
* packages/calypso-e2e/src/rest-api-client.ts
- fix empty array check

* packages/calypso-e2e/src/rest-api-client.ts
- fix another instance of comparison

* packages/calypso-e2e/src/rest-api-client.ts
- fix if logic.
- add output.
- add comments.

* packages/calypso-e2e/src/rest-api-client.ts
- truthy check

* packages/calypso-e2e/src/test/rest-api-client.invites.test.ts
- fix test
  • Loading branch information
worldomonation authored Mar 22, 2023
1 parent 1418db6 commit 1d87e23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 9 additions & 4 deletions packages/calypso-e2e/src/rest-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,20 @@ export class RestAPIClient {
params
);

// This handles API errors such as `unauthorized`.
if ( response.hasOwnProperty( 'error' ) ) {
throw new Error(
`${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }`
);
}

if ( response.errors === [] ) {
console.log( response );
throw new Error( `Failed to create invite: ${ response.errors }` );
// This handles "errors" relating to the invite itself and can be an array.
// For instance, if a user tries to invite itself, or invite an already added user.
if ( response.errors.length ) {
for ( const err of response.errors ) {
console.error( `${ err.code }: ${ err.message }` );
}
throw new Error( `Failed to create invite due to ${ response.errors.length } errors.` );
}

return response;
Expand Down Expand Up @@ -1035,7 +1040,7 @@ export class RestAPIClient {
if ( response.hasOwnProperty( 'error' ) && response.error === 'not_found' ) {
console.info( `Widget ${ widgetID } not found.` );
return;
} else if ( response === [] ) {
} else if ( response.length === 0 ) {
console.info( `Deleted widget ${ widgetID }.` );
} else if ( response.id === widgetID ) {
console.info( `Deactivated widget ${ widgetID }` );
Expand Down
17 changes: 7 additions & 10 deletions packages/calypso-e2e/src/test/rest-api-client.invites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe( 'RestAPIClient: createInvite', function () {
expect( response.errors.length ).toBe( 0 );
} );

test( 'Invite cannot be created', async function () {
test( 'Invite is rejected due to existing user email', async function () {
const testSuccessfulEmails = [ '[email protected]' ];
const testFailedEmails = [ '[email protected]' ];
const role = 'Editor' as Roles;
Expand All @@ -126,14 +126,11 @@ describe( 'RestAPIClient: createInvite', function () {

nock( requestURL.origin ).post( requestURL.pathname ).reply( 200, testResponse );

const response: NewInviteResponse = await restAPIClient.createInvite( siteID, {
email: testSuccessfulEmails.concat( testFailedEmails ),
role: role,
} );
expect( response.sent ).toBeInstanceOf( Array );
expect( response.sent.length ).toBe( 1 );
expect( response.sent ).toEqual( testSuccessfulEmails );
expect( response.errors.length ).toBe( 1 );
expect( response.errors ).toEqual( testFailedEmails );
await expect( () =>
restAPIClient.createInvite( siteID, {
email: testSuccessfulEmails.concat( testFailedEmails ),
role: role,
} )
).rejects.toThrowError();
} );
} );

0 comments on commit 1d87e23

Please sign in to comment.