Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't find a to make findAllByRole work but findAllbyText works fine. #254

Closed
sanchitos opened this issue Oct 15, 2021 · 2 comments
Closed

Comments

@sanchitos
Copy link

Hi, I really don't know why trying to find by role throws an error, but findAllByText works perfectly, they are querying the same screen elements.

"@testing-library/angular": "^10.11.0",
"@testing-library/jest-dom": "^5.14.1",
"@angular/cli": "~10.2.0",

Component:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { JobsService } from '@app/shared/jobs.service';
import { Division } from '@app/shared/shared.models';

@Component({
  selector: 'app-home-career-oportunities',
  templateUrl: './career-oportunities.component.html',
  styleUrls: ['./career-oportunities.component.css'],
})
export class CareerOportunitiesComponent implements OnInit {
  dedicated = {} as Division;
  intermodal = {} as Division;
  noCdl = {} as Division;
  otr = {} as Division;

  constructor(private jobsService: JobsService, private router: Router) {}

  ngOnInit(): void {
    void this.jobsService.divisions().then((apiDivisions) => {
      this.dedicated = apiDivisions.find((c) => c.JobType === 'DEDICATED');
      this.intermodal = apiDivisions.find((c) => c.JobType === 'INTERMODAL');
      this.noCdl = apiDivisions.find((c) => c.JobType === 'NO_CDL');
      this.otr = apiDivisions.find((c) => c.JobType === 'OVER_THE_ROAD');
    });
  }
}

HTML

    <ul class="popu-category-bullets">
              <li class="text-dark" *ngFor="let bullet of dedicated.JobBullets">
                {{ bullet }}
              </li>
            </ul>

Test:

describe('Rendering...', () => {
  test('Render Component', async () => {
    const divisions2: Division[] = [
      {
        JobType: 'INTERMODAL',
        JobBullets: ['Local Routes', 'Flexible Schedules', 'Competitive Pay'],
        Description: '',
      },
      { JobType: 'NO_CDL', JobBullets: ['We Train', 'We Hire', 'We Pay'], Description: '' },
      {
        JobType: 'OVER_THE_ROAD',
        JobBullets: ['Great Miles', 'Competitive Pay', 'Explore the Country'],
        Description: '',
      },
      {
        JobType: 'DEDICATED',
        JobBullets: ['Regular Routes', 'Consistent Miles', 'Great Pay'],
        Description: '',
      },
    ];
    const jobService = createMock(JobsService);
    jobService.divisions = jest.fn(() => Promise.resolve(divisions2));

    await render(CareerOportunitiesComponent, {
      componentProviders: [
        {
          provide: JobsService,
          useValue: jobService,
        },
      ],
      imports: [ SomeModules ]
      ],
    });
    // const dedicated = await screen.findAllByRole('listitem');
    // throws error!
    // console.log(dedicated);
    const dedicated = await screen.findAllByText('Regular Routes');
    // works OK
  });
});

What screen.debug() shows:

image

Thanks

@sanchitos sanchitos changed the title [Angular] Can't find a to make findAllByRole work but findAllbyText works fine. Can't find a to make findAllByRole work but findAllbyText works fine. Oct 15, 2021
timdeschryver added a commit that referenced this issue Oct 16, 2021
@timdeschryver
Copy link
Member

I reproduced the test, but it seems to work here.
#255

@sanchitos
Copy link
Author

I have no idea what's going on. But I will attempt to setup testing library again and see if the issue goes away.

If I can't make it by tuesday I will close the issue.

Thank you very much!

timdeschryver added a commit that referenced this issue Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants