Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(web) Add tests for asset repository #680

Merged
merged 3 commits into from
Sep 16, 2022
Merged

test(web) Add tests for asset repository #680

merged 3 commits into from
Sep 16, 2022

Conversation

alextran1502
Copy link
Contributor

@alextran1502 alextran1502 commented Sep 12, 2022

No description provided.

@alextran1502 alextran1502 marked this pull request as ready for review September 13, 2022 02:41
expect(result.buckets.length).toEqual(2);
});

it('get asset count by user id', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbaez Can you help me with some feedback on this specific test. What are other property on this specific API/Service I should have tested?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the code in the service, you have 100% coverage for getAssetCountByUserId:

getAssetCountByUserId(authUser: AuthUserDto): Promise<AssetCountByUserIdResponseDto> {
    return this._assetRepository.getAssetCountByUserId(authUser.id);
  }

Since it only accepts an authUser, I don't think there would be anything else to add. If that accepted a userId, then it would probably need to test what happens if the user doesn't exist, or you are not allowed to get the asset count for that user etc.

@alextran1502 alextran1502 merged commit 2c2ea24 into main Sep 16, 2022
@alextran1502 alextran1502 deleted the dev/add-tests branch September 16, 2022 21:47
Copy link
Contributor

@jbaez jbaez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests, good to tests to cover new things in the code 🎉
I left some comments, let me know if you have any further questions 😀

// });
expect(result.userId).toEqual(authUser.id);
expect(result.resizePath).toEqual('');
expect(result.webpPath).toEqual('');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing these expectations on individual properties of result, it would probably be easier to do:

expect(result).toBe(assetEntity);

There are 2 things we want to check here:

  • the assetEntity returned by the assetRepository is returned by the service (like you have or example above)
  • the assetRepository is called with the expected params.

For the second there would need to be an expect(createAssetDto. create).toHaveBeenCalledWith():

      createAssetDto,
      authUser.id,
      originalPath,
      mimeType,
      checksum,

const assets = _getAssets();

assetRepositoryMock.getAllByDeviceId.mockImplementation(() =>
Promise.resolve<string[]>(Array.from(assets.map((asset) => asset.deviceAssetId))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is tempting to use a loop to get the IDs here. In this case it doesn't seem too bad. However, the general rule is to not use loops or conditions inside the test, makes it more simple and easier to read. Hard-coding the array of strings would be preferable.

expect(result.length).toEqual(1);
expect(result[0]).toEqual('4967046344801');
expect(result.length).toEqual(2);
expect(result).toEqual(assets.map((asset) => asset.deviceAssetId));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, you could define a const assetsResult = ["with", "hardCoded", "ids"] and return in the the promise and use it here in the expect

timeGroup: TimeGroupEnum.Month,
});

expect(result.totalCount).toEqual(assetCountByTimeBucket.reduce((a, b) => a + b.count, 0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a hardcoded value, no loops allowed 😃

expect(result.buckets.length).toEqual(2);
});

it('get asset count by user id', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the code in the service, you have 100% coverage for getAssetCountByUserId:

getAssetCountByUserId(authUser: AuthUserDto): Promise<AssetCountByUserIdResponseDto> {
    return this._assetRepository.getAssetCountByUserId(authUser.id);
  }

Since it only accepts an authUser, I don't think there would be anything else to add. If that accepted a userId, then it would probably need to test what happens if the user doesn't exist, or you are not allowed to get the asset count for that user etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants