DEV Community

Cover image for Using Jest to bulletproof a TypeScript Class - Part 2
João Textor
João Textor

Posted on

Using Jest to bulletproof a TypeScript Class - Part 2

The second and final part of our testing suite is here!
Check out the first part HERE.

We will continue testing our generate method.

Here we'll test generating a code with 2 groups and the default separator.

it("Should generate a code with 2 groups and the default group separator", () => {
    sut = new CodeGenerator(5, {
    groups: 2,
    });

    const code = sut.generate();    
    expect(code).toHaveLength(1);
    expect(code[0]).toHaveLength(11);
    expect(code[0]![5]).toBe("-");
});
Enter fullscreen mode Exit fullscreen mode

Our code will still have the length of 1, and the first index will have a length of 11, since the key has 2 groups of 5 characters, additionally to the separator "-".

Now we will modify our configuration, this time using the "_" character as the group separator. We're testing if the CodeGenerator respects our choice of separator.

it("Should generate a code with 2 groups and the '_' 'groupSeparator'", () => {
    sut = new CodeGenerator(5, {
        groups: 2,
        groupSeparator: "_",
    });
    const code = sut.generate();

    expect(code).toHaveLength(1);
    expect(code[0]).toHaveLength(11);
    expect(code[0]![5]).toBe("_");
});
Enter fullscreen mode Exit fullscreen mode

The next test will analyze the generation of 2 keys at the same time.

it("Should generate 2 random keys", () => {
    sut = new CodeGenerator(5, {
        numberOfKeys: 2,
    });
    const code = sut.generate();

    expect(code).toHaveLength(2);
    expect(code[0]).toHaveLength(5);
    expect(code[1]).toHaveLength(5);
});
Enter fullscreen mode Exit fullscreen mode

As expected, the code Array will have 2 items, and each of them a length of 5.

We also need to test generating keys with a different characterType, which is done in the following test:

it("Should generate 1 key with only numbers", () => {
    sut = new CodeGenerator(5, {
        characterType: "Numbers",
    });
    const code = sut.generate();
    const numberCode = Number(code[0]);

    expect(code).toHaveLength(1);
    expect(numberCode).not.toBeNaN();
});
Enter fullscreen mode Exit fullscreen mode

Here we converted our generated code (which is a string inside an Array) into a number, and test if the result is an actual number or it returned NaN in the type conversion.

Our next test will analyze if our code will give priority to the length of groupFormat over the length informed in the first property when instantiating a class.

it("Should generate a key with 'groupFormat' length instead of the length informed.", () => {
    sut = new CodeGenerator(5, {
        characterType: "LettersAndNumbers",
        groupFormat: "NNLNLLN",
    });
    const code = sut.generate();

    expect(code[0]).toHaveLength(7);
});
Enter fullscreen mode Exit fullscreen mode

Our groupFormat have a length of 7, but we passed the number 5 for the property of numbersOfCharacters. Our logic demands that when the groupFormat is informed, the output group should have the length of the group format informed.

Finally, to our last test, we will verify if the groupFormat informed by the developer is actually being respected in the output.

In short, if the grouptFormat is "LLNLN", the output should be something like this: "ZH8D2".

This is going to be a more complex test than the others, since we will have to convert our string key to an Array and iterate this Array to see if it is a number or not.

it("Should generate a key with the format informed in 'groupFormat' property. ", () => {
    const groupFormat = "LLNLN";
    const groupFormatArray = Array.from(groupFormat);
    sut = new CodeGenerator(5, {
        groupFormat: groupFormat,
    });
    const codes = sut.generate();
    const codeArray = Array.from(codes[0]!);

    for (let char in codeArray) {
        const codeChar = Number(codeArray[char]);
        if (groupFormatArray[char] === "L") {
          expect(codeChar).toBeNaN;
          return;
        }
    expect(codeChar).not.toBeNaN();
    }
});
Enter fullscreen mode Exit fullscreen mode

With this final test, we finished our suite of unit tests for the CodeGenerator class.

This is going to be the last post in this series. However, next week I will open a new series with an article on how I published the Easy Key Generator to npmjs.com as a library that can be used by anyone. Hit the follow button to stay tuned in future posts.

Talk to you all next week, and do not forget to react to this post and comment if you have any suggestions or to make any constructive criticisms :-)

Top comments (0)