Using Jest to bulletproof a TypeScript Class - Part 2
Tags: webdev, testing, typescript, jest
Published: 2023-09-26 22:00:00

The second and final part of our testing suite is here!
Check out the first part [HERE](https://dev.to/joaotextor/using-jest-to-bulletproof-a-typescript-class-part-1-hjo).

We will continue testing our `generate` method.

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

```js
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("-");
});
```

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.

```js
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("_");
});
```

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

```js
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);
});
```

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:

```js
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();
});
```

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.

```js
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);
});
```

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.

```js
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();
	}
});
```

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 :-)