From 9e44a84c0370750cbe2d7100a5e41f62a15dff21 Mon Sep 17 00:00:00 2001 From: James Rodewig Date: Tue, 20 Aug 2024 09:35:52 -0400 Subject: [PATCH] [DOCS-3274] Fix race condition in pagination tests (#49) --- __tests__/orders.test.ts | 8 ++++++++ __tests__/products.test.ts | 13 ++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/__tests__/orders.test.ts b/__tests__/orders.test.ts index 43ceb12..ecfb8f2 100644 --- a/__tests__/orders.test.ts +++ b/__tests__/orders.test.ts @@ -168,16 +168,24 @@ describe("Orders", () => { ); expect(firstResp.status).toEqual(200); expect(firstResp.body.results.length).toEqual(1); + expect(firstResp.body.nextToken).toBeDefined(); + // Get the second page of orders const secondResp = await req(app).get( `/customers/${customer.id}/orders?nextToken=${firstResp.body.nextToken}` ); expect(secondResp.status).toEqual(200); expect(secondResp.body.results.length).toEqual(1); + // Ensure the orders returned are different. expect(firstResp.body.results[0].createdAt).not.toEqual( secondResp.body.results[0].createdAt ); + + // Verify that the second page's nextToken is defined. + if (secondResp.body.nextToken) { + expect(secondResp.body.nextToken).toBeDefined(); + } }); it("returns a 400 if 'pageSize' is invalid", async () => { diff --git a/__tests__/products.test.ts b/__tests__/products.test.ts index b4de93f..8754419 100644 --- a/__tests__/products.test.ts +++ b/__tests__/products.test.ts @@ -57,20 +57,27 @@ describe("Products", () => { }); it("can paginate the list of products", async () => { - // Get the first page of orders. + // Get the first page of products. const firstResp = await req(app).get("/products?pageSize=1"); expect(firstResp.status).toEqual(200); expect(firstResp.body.results.length).toEqual(1); - // Get the second page of orders + expect(firstResp.body.nextToken).toBeDefined(); + + // Get the second page of products const secondResp = await req(app).get( `/products?nextToken=${firstResp.body.nextToken}` ); expect(secondResp.status).toEqual(200); expect(secondResp.body.results.length).toEqual(1); - // Ensure the orders returned are different. + // Ensure the products returned are different. expect(firstResp.body.results[0].name).not.toEqual( secondResp.body.results[0].name ); + + // Verify that the second page's nextToken is defined. + if (secondResp.body.nextToken) { + expect(secondResp.body.nextToken).toBeDefined(); + } }); it("returns a 400 if 'pageSize' is invalid", async () => {