Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ describe('injectIsFetching', () => {
expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
})

it('should be able to filter by queryKey', async () => {
const key1 = queryKey()
const key2 = queryKey()

@Component({
template: `<div>fetching: {{ isFetching() }}</div>`,
})
class Page {
readonly query1 = injectQuery(() => ({
queryKey: key1,
queryFn: () => sleep(10).then(() => 'test1'),
}))
readonly query2 = injectQuery(() => ({
queryKey: key2,
queryFn: () => sleep(100).then(() => 'test2'),
}))
readonly isFetching = injectIsFetching({ queryKey: key1 })
}

const rendered = await render(Page)

await vi.advanceTimersByTimeAsync(0)
rendered.fixture.detectChanges()
expect(rendered.getByText('fetching: 1')).toBeInTheDocument()

await vi.advanceTimersByTimeAsync(11)
rendered.fixture.detectChanges()
expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
})

describe('injection context', () => {
it('should throw NG0203 with descriptive error outside injection context', () => {
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ describe('injectIsMutating', () => {
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
})

it('should be able to filter by mutationKey', async () => {
const key1 = queryKey()
const key2 = queryKey()

@Component({
template: `<div>mutating: {{ isMutating() }}</div>`,
})
class Page {
readonly mutation1 = injectMutation(() => ({
mutationKey: key1,
mutationFn: () => sleep(10).then(() => 'data1'),
}))
readonly mutation2 = injectMutation(() => ({
mutationKey: key2,
mutationFn: () => sleep(100).then(() => 'data2'),
}))
readonly isMutating = injectIsMutating({ mutationKey: key1 })
}

const rendered = await render(Page)

rendered.fixture.componentInstance.mutation1.mutate()
rendered.fixture.componentInstance.mutation2.mutate()

await vi.advanceTimersByTimeAsync(0)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 1')).toBeInTheDocument()

await vi.advanceTimersByTimeAsync(11)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
})

describe('injection context', () => {
it('should throw NG0203 with descriptive error outside injection context', () => {
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ describe('injectIsRestoring', () => {
expect(isRestoring()).toBe(true)
})

it('should reactively reflect changes to the provided signal', () => {
const restoringSignal = signal(true)

TestBed.configureTestingModule({
providers: [provideIsRestoring(restoringSignal.asReadonly())],
})

const isRestoring = TestBed.runInInjectionContext(() => {
return injectIsRestoring()
})

expect(isRestoring()).toBe(true)

restoringSignal.set(false)
expect(isRestoring()).toBe(false)

restoringSignal.set(true)
expect(isRestoring()).toBe(true)
})

it('should be usable outside injection context when passing an injector', () => {
const isRestoring = injectIsRestoring({
injector: TestBed.inject(Injector),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ describe('injectMutation', () => {
await expect(() => mutateAsync()).rejects.toThrow(err)
})

it('should resolve mutateAsync with the value returned from mutationFn', async () => {
const key = queryKey()
const { mutateAsync } = TestBed.runInInjectionContext(() => {
return injectMutation(() => ({
mutationKey: key,
mutationFn: (params: string) => sleep(10).then(() => params),
}))
})

const promise = mutateAsync('Mock data')
await vi.advanceTimersByTimeAsync(11)

await expect(promise).resolves.toBe('Mock data')
})

describe('injection context', () => {
it('should throw NG0203 with descriptive error outside injection context', () => {
const key = queryKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,50 @@ describe('injectQueries', () => {
expect(results.length).toBeGreaterThanOrEqual(2)
})

it('should reflect error state when one of the queries rejects', async () => {
const key1 = queryKey()
const key2 = queryKey()

@Component({
template: `
<div>
status1: {{ result()[0].status() }}, error1:
{{ result()[0].error()?.message ?? 'none' }}
</div>
<div>
status2: {{ result()[1].status() }}, data2:
{{ result()[1].data() ?? 'none' }}
</div>
`,
})
class Page {
readonly result = injectQueries(() => ({
queries: [
{
queryKey: key1,
queryFn: () =>
sleep(10).then(() => Promise.reject(new Error('Some error'))),
retry: false,
},
{
queryKey: key2,
queryFn: () => sleep(10).then(() => 2),
},
],
}))
}

const rendered = await render(Page)

await vi.advanceTimersByTimeAsync(11)
rendered.fixture.detectChanges()

expect(
rendered.getByText('status1: error, error1: Some error'),
).toBeInTheDocument()
expect(rendered.getByText('status2: success, data2: 2')).toBeInTheDocument()
})

describe('isRestoring', () => {
it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
const key1 = queryKey()
Expand Down
Loading