Page MenuHomePhabricator

[lib] Write some tests for `text-utils:pluralize(...)`
ClosedPublic

Authored by atul on Jun 22 2022, 11:33 AM.
Tags
None
Referenced Files
F3374609: D4327.id13679.diff
Tue, Nov 26, 4:25 PM
F3374181: D4327.diff
Tue, Nov 26, 2:18 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM
Unknown Object (File)
Thu, Oct 31, 12:07 PM

Details

Summary

Suggested by @palys-swm:

Also, this is a great function to have some tests.

https://phab.comm.dev/D4317#121940

Took like a minute. Not sure if it helps much, but it doesn't hurt?

Test Plan

Ran tests

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

lib/utils/text-utils.js
7 ↗(On Diff #13679)

Without this change, we got nonsense output when maxNumberOfNouns was 0:

pluralize(['cat', 'dog', 'sheep'], 0) was returning:

Received: "cat, dog and 4 others"
atul requested review of this revision.Jun 22 2022, 11:38 AM

Best for a separate diff, but wondering if there a good way we can:

  1. Add this test to CI, and
  2. Run this test in lint-staged if the relevant file is changed?

Add this test to CI

yarn workspace lib test is already included in the CI

Run this test in lint-staged if the relevant file is changed?

Will look at adding unit tests to pre-commit hooks

  1. Cool, might be a good thing to create a task for that
  2. lint-staged should be fast, so we should probably avoid globally running jest for the whole project (unless that's fast / there's a way to make it fast)

Tests look good but there's some redundancy that can be removed.

lib/utils/text-utils.js
7 ↗(On Diff #13679)

Good call on this. If maxNumberOfNouns === 0, I can't think of a reason to return anything but an empty string.

lib/utils/text-utils.test.js
10 ↗(On Diff #13679)

This test covers the case when nouns.length === 3 and maxNumberOfNouns === 3 (because of the default parameter).

This case is repeated on line 13:

expect(pluralize(['cat', 'dog', 'sheep'], 3)).toBe('cat, dog, and sheep');

Not sure if you included both of these to test the default parameter vs. the explicit parameter, but in my opinion the tests are equivalent since we know the behavior of default parameters in JavaScript. Additionally, since the expected output is the same with the only difference being the values in nouns, only one test is needed since nouns will always be an array of strings.

11 ↗(On Diff #13679)

This tests the case when nouns.length === 4 and maxNumberOfNouns === 3 (default parameter, again).

This case is repeated in the test on lines 18-20:

expect(pluralize(['cat', 'dog', 'sheep', 'moose'])).toBe(
   'cat, dog, and 2 others',
);

I think you only need one of these tests by the same reasoning above.

This revision is now accepted and ready to land.Jun 22 2022, 12:19 PM

Not sure if you included both of these to test the default parameter vs. the explicit parameter,

Yeah that's why I included them. The redundancy was intentional

Oh, okay my bad! I only made those suggestions since if this is part of the CI build we want it to run as fast as possible so we should eliminate redundant tests.

In D4327#122115, @yayabosh wrote:

Oh, okay my bad! I only made those suggestions since if this is part of the CI build we want it to run as fast as possible so we should eliminate redundant tests.

No worries, I think that for tests we should err on the side of covering more cases vs eliminating redundancy.

As for CI build times, the cost of an additional unit test is orders of magnitude too small to make a difference

This revision now requires review to proceed.Jun 22 2022, 1:47 PM

Thanks for creating these tests!

I think they can be improved by using describe / it. We would have a single test suite for text utils, then a separate ones for each function, and then a separate test for each case. Something like this:

describe('Text utils", () => {
  describe('pluralize function', () => {
    it('should return a single word when a single word is given, () => {
      expect(pluralize(['a'])).toBe('a');
    };
    ...
  };
  ...
};

There are a couple of advantages:

  1. We can clearly see which tests pass - instead of seeing only the first failed test
  2. An intention of a test is explained
  3. Tests are grouped together into logical sections

On the other hand creating these would take more time, but it should be worth it if we plan to analyze test reports.

What do you think?

This revision is now accepted and ready to land.Jun 23 2022, 2:27 AM
In D4327#122343, @palys-swm wrote:

I think they can be improved by using describe / it. We would have a single test suite for text utils, then a separate ones for each function, and then a separate test for each case. Something like this:

describe('Text utils", () => {
  describe('pluralize function', () => {
    it('should return a single word when a single word is given, () => {
      expect(pluralize(['a'])).toBe('a');
    };
    ...
  };
  ...
};

There are a couple of advantages:

  1. We can clearly see which tests pass - instead of seeing only the first failed test
  2. An intention of a test is explained
  3. Tests are grouped together into logical sections

On the other hand creating these would take more time, but it should be worth it if we plan to analyze test reports.

What do you think?

All of these reasons sound great, I'll give it a shot and update this diff in place

rebase after cherrypicking and before addressing feedback

@palys-swm thanks for the suggestion, this looks a lot better

43cfc2.png (682×1 px, 503 KB)

and each test is "self-documenting"

address feedback from @palys-swm

rebase after services build fix