Skip to content

Commit

Permalink
chore(angular-query): add injection context tests (TanStack#7993)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv authored Aug 30, 2024
1 parent 4b2c8c5 commit cc20045
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestBed } from '@angular/core/testing'
import { afterEach } from 'vitest'
import { Injector } from '@angular/core'
import { QueryClient, injectInfiniteQuery, provideAngularQuery } from '..'
import { expectSignals, infiniteFetcher } from './test-utils'

Expand Down Expand Up @@ -59,4 +60,31 @@ describe('injectInfiniteQuery', () => {
status: 'success',
})
})

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectInfiniteQuery(() => ({
queryKey: ['injectionContextError'],
queryFn: infiniteFetcher,
initialPageParam: 0,
getNextPageParam: () => 12,
}))
}).toThrowError(/NG0203(.*?)injectInfiniteQuery/)
})

test('can be used outside injection context when passing an injector', () => {
const query = injectInfiniteQuery(
() => ({
queryKey: ['manualInjector'],
queryFn: infiniteFetcher,
initialPageParam: 0,
getNextPageParam: () => 12,
}),
TestBed.inject(Injector),
)

expect(query.status()).toBe('pending')
})
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'
import { beforeEach, describe, expect } from 'vitest'
import { Injector } from '@angular/core'
import {
QueryClient,
injectIsFetching,
Expand Down Expand Up @@ -34,4 +35,18 @@ describe('injectIsFetching', () => {
flush()
expect(isFetching()).toStrictEqual(0)
}))

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsFetching()
}).toThrowError(/NG0203(.*?)injectIsFetching/)
})

test('can be used outside injection context when passing an injector', () => {
expect(
injectIsFetching(undefined, TestBed.inject(Injector)),
).not.toThrow()
})
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeEach, describe } from 'vitest'
import { TestBed, fakeAsync, tick } from '@angular/core/testing'
import { Injector } from '@angular/core'
import {
QueryClient,
injectIsMutating,
Expand Down Expand Up @@ -38,4 +39,18 @@ describe('injectIsMutating', () => {
expect(isMutating()).toBe(1)
})
}))

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsMutating()
}).toThrowError(/NG0203(.*?)injectIsMutating/)
})

test('can be used outside injection context when passing an injector', () => {
expect(
injectIsMutating(undefined, TestBed.inject(Injector)),
).not.toThrow()
})
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, input, signal } from '@angular/core'
import { Component, Injector, input, signal } from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { describe, expect, test, vi } from 'vitest'
import { By } from '@angular/platform-browser'
Expand Down Expand Up @@ -173,5 +173,22 @@ describe('injectMutationState', () => {

expect(spans).toEqual(['success', 'error'])
})

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectMutationState()
}).toThrowError(/NG0203(.*?)injectMutationState/)
})

test('can be used outside injection context when passing an injector', () => {
const injector = TestBed.inject(Injector)
expect(
injectMutationState(undefined, {
injector,
}),
).not.toThrow()
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Component, Injectable, inject, input, signal } from '@angular/core'
import {
Component,
Injectable,
Injector,
inject,
input,
signal,
} from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { describe, expect, vi } from 'vitest'
import { By } from '@angular/platform-browser'
Expand Down Expand Up @@ -494,4 +501,27 @@ describe('injectMutation', () => {
).toEqual('test')
expect(errorSpy).not.toHaveBeenCalled()
})

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectMutation(() => ({
mutationKey: ['injectionContextError'],
mutationFn: () => Promise.resolve(),
}))
}).toThrowError(/NG0203(.*?)injectMutation/)
})

test('can be used outside injection context when passing an injector', () => {
expect(() => {
injectMutation(
() => ({
mutationKey: ['injectionContextError'],
mutationFn: () => Promise.resolve(),
}),
TestBed.inject(Injector),
)
}).not.toThrow()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,27 @@ describe('injectQuery', () => {

expect(fixture.componentInstance.query.data()).toEqual('test name 2')
}))

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectQuery(() => ({
queryKey: ['injectionContextError'],
queryFn: simpleFetcher,
}))
}).toThrowError(/NG0203(.*?)injectQuery/)
})

test('can be used outside injection context when passing an injector', () => {
const query = injectQuery(
() => ({
queryKey: ['manualInjector'],
queryFn: simpleFetcher,
}),
TestBed.inject(Injector),
)

expect(query.status()).toBe('pending')
})
})
})

0 comments on commit cc20045

Please sign in to comment.