Mock Response Test Case Bug Help

Hello,

I’m currently trying to pass this test case called “return nest set of items after next token is requested”. I cant seem to find why this test case is expecting different number of items than the 4 items required of it in initialResponse. It looks like the mock response isn’t correct. How should I update the mock response? I couldnt find the typescript forum hence why I’m here in javascript to get help. I’m testing the getBeeHistory method Thanks

Code down below:

 test.ts file:

 const ITEMS_SMALL_FIRST_PAGE = [
   {
     version: 1,
     constId: CONST_ID,
     noIndex: false,
     reason: "reason",
     alias: "alias",
     timestamp: 0,
   },
   {
     version: 2,
     constId: CONST_ID,
     noIndex: true,
     reason: "reason2",
     alias: "alias",
     timestamp: 1,
   },
   {
     version: 3,
     constId: CONST_ID,
     noIndex: false,
     reason: "reason3",
     alias: "alias",
     timestamp: 2,
   },
   {
     version: 4,
     constId: CONST_ID,
     noIndex: true,
     reason: "reason4",
     alias: "alias",
     timestamp: 3,
   },
 ];
  
 const ITEMS_SMALL_SECOND_PAGE = [
   {
     version: 5,
     constId: CONST_ID,
     noIndex: false,
     reason: "reason5",
     alias: "alias",
     timestamp: 4,
   },
 ];
 
 
 const NEXT_TOKEN =
 "eyJ2ZXJzaW9uIjo1LCJjb25zdElkIjoibm0wMDAwMDAxIiwibm9JbmRleCI6ZmFsc2UsInJlYXNvbiI6InJlYXNvbjUiLCJhbGlhcyI6ImFsaWFzIiwidGltZXN0YW1wIjo0fQ";
 
  
 const NEXT_TOKEN_SMALL =
 "eyJ2ZXJzaW9uIjo1LCJjb25zdElkIjoibm0wMDAwMDAxIiwibm9JbmRleCI6ZmFsc2UsInJlYXNvbiI6InJlYXNvbjUiLCJhbGlhcyI6ImFsaWFzIiwidGltZXN0YW1wIjo0fQ";
  
  
   test("return next set of items after next token is requested", async () => {
     dynamoResponseMock.mockResolvedValue({
       Items: ITEMS_SMALL_FIRST_PAGE,
       nextToken: NEXT_TOKEN,
     });
 
     const initialRequest: GetBeeHistoryRequest = {
       constId: CONST_ID,
       pageSize: ITEMS_SMALL_FIRST_PAGE.length,
     };
 
     const response: GetBeeHistoryResponse | null =
       await beeClient.getBeeHistory(initialRequest);
 
     expect(response).toEqual({
       items: ITEMS_SMALL_FIRST_PAGE,
       nextToken: NEXT_TOKEN_SMALL,
     });
 
     const nextResponse = await beeClient.getBeeHistory({
       ...initialRequest,
       constId: CONST_ID,
       pageSize: ITEMS_SMALL_SECOND_PAGE.length,
     });
 
     expect(nextResponse).toBe({ items: ITEMS_SMALL_SECOND_PAGE });
   });
  
  
  
  
  
  
 beeClient.ts method:
  
 public async getBeeHistory(
     itemId: GetBeeHistoryRequest
   ): Promise<GetBeeHistoryResponse | null> {
     const primaryKeyColumn = "constId";
     const pageSize = itemId.pageSize ?? BeeClient.DEFAULT_PAGE_SIZE;
  
     if (!isValidConst(itemId.constId)) {
       return Promise.reject(Error(`Invalid constId: ${itemId.constId}`));
     }
  
     if (pageSize == 0) {
       return Promise.reject(Error(`Invalid pageSize: ${pageSize}`));
     }
  
     let exclusiveStartKey = undefined;
     try {
       if (itemId.nextToken) {
         exclusiveStartKey = fromTokenString(itemId.nextToken);
       }
     } catch {
       return Promise.reject(Error(`Invalid nextToken: ${itemId.nextToken}`));
     }
  
     const command: DynamoDB.DocumentClient.QueryInput = {
       TableName: this.tableName,
       KeyConditionExpression: "#p = :p",
       ConsistentRead: false,
       ExpressionAttributeNames: {
         "#p": primaryKeyColumn,
       },
       ExpressionAttributeValues: {
         ":p": itemId.constId,
       },
       Limit: pageSize,
       ExclusiveStartKey: exclusiveStartKey,
     };
  
     const { Items, LastEvaluatedKey } = await this.documentClient
       .query(command)
       .promise();
  
     if (!Items || Items.length === 0) {
       return null;
     }
  
     return {
       items: Items as Record[],
       nextToken: LastEvaluatedKey ? toTokenString(LastEvaluatedKey) : undefined,
     };
   }
  
  
 const toTokenString = (key: DynamoDB.Key): string =>
   Buffer.from(JSON.stringify(key)).toString("base64");
 const fromTokenString = (token: string) =>
   JSON.parse(Buffer.from(token, "base64").toString());
  
 const isValidConst = (constId: string): boolean =>
   constId.match(/^(nm|tt)\d{7,}$/) ? true : false;
 
 
 
 
 Interface from GetBeeHistory.ts file: 
 import { Record } from "./Record";
 
 export type GetBeeHistoryRequest = {
   constId: string;
   pageSize?: number;
   nextToken?: string;
 };
 
 export type GetBeeHistoryResponse = {
   items: Record[];
   nextToken?: string;
 };
 
 
 
 Record.ts file:
 
 export type Record = {
   version: number;
   constId: string;
   noIndex: boolean;
   reason: string;
   alias: string;
   timestamp: number;
 };

Hey @yak78 !

Hmmm, I think more information is needed. Do you mind dropping a link to a GH repo, as well as the exact steps you take to get the unexpected behaviour? Also, an error message would be helpful!

Thanks,
Michael