Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2299

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T04:07:06+00:00 2024-11-26T04:07:06+00:00

Using Jest to bulletproof a TypeScript Class – Part 2

  • 61k

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 🙂

jesttestingtypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.