Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
Unit-Test for a switch statement on Angular

I'm new to angular unit testing and I don't know how to test this switch:

  displayBadge(text: any) {
    switch (text) {
      case 'blabla':
        return 'badge badge-pill badge-success';
      case 'lalala':
        return 'badge badge-pill badge-secondary';
      case 'uuuu':
        return 'badge badge-pill badge-warning';
      case 'ooooo':
        return 'badge badge-pill badge-warning';
      case 'eeee':
        return 'badge badge-pill badge-warning';
      case 'zzzzz':
        return 'badge badge-pill badge-dark';
      case 'aaaaa':
        return 'badge badge-pill badge-dark';
      case 'qqqqq':
        return 'badge badge-pill badge-success';
      case 'ccccc':
        return 'badge badge-pill badge-warning';
      case 'rrrrr':
        return 'badge badge-pill badge-success';
      case 'ttttt':
        return 'badge badge-pill badge-warning';
      case 'fffff':
        return 'badge badge-pill badge-success';
      default:
        return '';

I'm using Simontest plugin on VSCODE and the code coverage always tells me I'm missing like 80% of the coverage, which is this switch.

I've tried this way on my spec.ts file:

it('text aaaaa', () => {
    component.text ='aaaaa'
    component.displayBadge(component.text);
    expect('badge badge-pill badge-secondary');
  });

but it doesn't work.

text is defined like this in my ts:

@Input()
  text!: any;

I've seen there are others posts about testing a switch, I've tried everything I've seen here on stack and in general on Google, but SimonTest always tells me this function is not tested.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Since displayBadge() is returning the value you are testing for, something like this should work:

it('text aaaaa', () => {
 expect(component.displayBadge('aaaaa')).toEqual('badge badge-pill 
 badge-secondary');
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!