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

Tests for google-sheets.ts #49

Merged
merged 12 commits into from
Mar 12, 2024
Merged

Tests for google-sheets.ts #49

merged 12 commits into from
Mar 12, 2024

Conversation

jasnoo
Copy link
Contributor

@jasnoo jasnoo commented Feb 29, 2024

Added tests to the functions that are used by summarize handler for now, can add on tests to the other functions later but wanted to try to move forward with updating summarize so pushing this out for now. Also updated getTotalRows functionality to handle when the spreadsheet has less comments than requested.

@jasnoo jasnoo requested a review from namanaman February 29, 2024 16:55
Copy link
Contributor

@namanaman namanaman left a comment

Choose a reason for hiding this comment

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

Nice nice nice

src/libs/google-sheets.test.ts Outdated Show resolved Hide resolved

describe('getAuthClient', () => {
it('should successfully create and authorize a Google Sheets API client', async () => {
(google.sheets as jest.Mock).mockImplementationOnce(
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we had already mocked this on L24 above?

expect(google.auth.JWT).toHaveBeenCalledWith(
process.env.CLIENT_EMAIL,
null,
process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'),
Copy link
Contributor

Choose a reason for hiding this comment

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

One thing we could do to test this key logic properly is to have the test value in the env set including test-key\\n and then see if we are calling this JWT with just test-key\n. Then we are testing the desired behavior (removing an extra slash) more so than the implementation logic (using replace)

src/libs/google-sheets.test.ts Outdated Show resolved Hide resolved
'$description',
async ({ n, expectedRange, expectedComments }) => {
MOCK_SHEETS.spreadsheets.values.get
.mockResolvedValueOnce({ data: { values: [['3']] } }) // calls getTotalRows
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if it makes more sense to mock getTotalRows here. That way, the dev doesn't need to know the order in which we are calling spreadsheets.values.get in the getLastNComments function in order to mock properly. Could imagine us adding another call to sheets in the function that would then mess up the logic here. We could separately test getTotalRows in a future PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up going this route since getTotalRows isn't exported from google-sheets.ts (and I didn't think I should export it for the sake of testing). Any recs on how to achieve this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm good point. I personally feel like we're better off exporting this so it can be properly unit tested. I feel like we get more value from having a unit test to increase coverage vs. what we lose in exposing this function to other modules via export. There could be a best of both worlds solution that I am missing, though. Maybe a good question to ask on the engineering channel!

src/libs/google-sheets.test.ts Show resolved Hide resolved
src/libs/google-sheets.test.ts Outdated Show resolved Hide resolved
src/libs/google-sheets.test.ts Show resolved Hide resolved
src/libs/google-sheets.ts Show resolved Hide resolved
src/libs/google-sheets.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@namanaman namanaman left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for going through that discussion with me

@jasnoo
Copy link
Contributor Author

jasnoo commented Mar 12, 2024

Making a note that it could be good to do a check for getLastNComments to check if 1 row is returned in the count (since there could be a sheet that just has a header.

@jasnoo jasnoo merged commit 7134de1 into main Mar 12, 2024
1 check passed
@jasnoo jasnoo deleted the test-lib-google-sheets branch March 12, 2024 18:45
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