dariusz
dariusz

Reputation: 593

Unit tests for async url parameters in Svelte and Jest

In Svelte onMount I'm doing following logic.

onMount(async () => {
    projectId = await new URLSearchParams(window.location.search).get('p');
    jobId = await new URLSearchParams(window.location.search).get('j');
    getJobs();
});

Then I have following test

test('will call getJobs endpoint on page load', () => {
    render(SingleJobView);
    setTimeout(() => {
      expect(getJobs).toHaveBeenCalled();
    }, 1000);
});

The problem is, that unit test will not pass without setTimeout. How can I test the projectId, jobId as mocked url parameters and getJobs endpoint call without setTimeout?

Upvotes: 0

Views: 15

Answers (0)

Related Questions