Skip to content

Commit

Permalink
feat(angular-query): add inject mutation test
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoperra committed Mar 3, 2024
1 parent 62bc39d commit a8ee8fd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Component, input, signal } from '@angular/core'
import { QueryClient } from '@tanstack/query-core'
import { TestBed } from '@angular/core/testing'
import { describe, expect, test, vi } from 'vitest'
import { By } from '@angular/platform-browser'
import { injectMutation } from '../inject-mutation'
import { provideAngularQuery } from '../providers'
import {
errorMutator,
expectSignals,
setSignalInputs,
setFixtureSignalInputs,
successMutator,
} from './test-utils'

Expand Down Expand Up @@ -314,30 +315,91 @@ describe('injectMutation', () => {

@Component({
selector: 'app-fake',
template: ``,
template: `
<button (click)="mutate()"></button>
<span>{{ mutation.data() }}</span>
`,
standalone: true,
})
class FakeComponent {
name = input.required<string>()

mutation = injectMutation(() => ({
mutationKey: ['fake', this.name()],
mutationFn: (params: string) => successMutator(params),
mutationFn: () => successMutator(this.name()),
}))

mutate(): void {
this.mutation.mutate()
}
}

const fixture = TestBed.createComponent(FakeComponent)
setSignalInputs(fixture.componentInstance, {
name: 'value',
})
fixture.detectChanges()
const { debugElement } = fixture
setFixtureSignalInputs(fixture, { name: 'value' })

fixture.componentInstance.mutation.mutate('myValue')
const button = debugElement.query(By.css('button'))
button.triggerEventHandler('click')

await resolveMutations()
fixture.detectChanges()

const text = debugElement.query(By.css('span')).nativeElement.textContent
expect(text).toEqual('value')
const mutation = mutationCache.find({ mutationKey: ['fake', 'value'] })
expect(mutation).toBeDefined()
expect(mutation!.options.mutationKey).toStrictEqual(['fake', 'value'])
})

test('should update options on required signal input change', async () => {
const mutationCache = queryClient.getMutationCache()

@Component({
selector: 'app-fake',
template: `
<button (click)="mutate()"></button>
<span>{{ mutation.data() }}</span>
`,
standalone: true,
})
class FakeComponent {
name = input.required<string>()

mutation = injectMutation(() => ({
mutationKey: ['fake', this.name()],
mutationFn: () => successMutator(this.name()),
}))

mutate(): void {
this.mutation.mutate()
}
}

const fixture = TestBed.createComponent(FakeComponent)
const { debugElement } = fixture
setFixtureSignalInputs(fixture, { name: 'value' })

const button = debugElement.query(By.css('button'))
const span = debugElement.query(By.css('span'))

button.triggerEventHandler('click')
await resolveMutations()
fixture.detectChanges()

expect(span.nativeElement.textContent).toEqual('value')

setFixtureSignalInputs(fixture, { name: 'updatedValue' })

button.triggerEventHandler('click')
await resolveMutations()
fixture.detectChanges()

expect(span.nativeElement.textContent).toEqual('updatedValue')

const mutations = mutationCache.findAll()
expect(mutations.length).toBe(2)
const [mutation1, mutation2] = mutations
expect(mutation1!.options.mutationKey).toEqual(['fake', 'value'])
expect(mutation2!.options.mutationKey).toEqual(['fake', 'updatedValue'])
})
})
12 changes: 12 additions & 0 deletions packages/angular-query-experimental/src/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type InputSignal, isSignal, untracked } from '@angular/core'
import { SIGNAL, signalSetFn } from '@angular/core/primitives/signals'
import type { ComponentFixture } from '@angular/core/testing'

export function simpleFetcher(): Promise<string> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -113,3 +114,14 @@ export function setSignalInputs<T extends NonNullable<unknown>>(
}
}
}

export function setFixtureSignalInputs<T extends NonNullable<unknown>>(
componentFixture: ComponentFixture<T>,
inputs: ToSignalInputUpdatableMap<T>,
options: { detectChanges: boolean } = { detectChanges: true },
) {
setSignalInputs(componentFixture.componentInstance, inputs)
if (options.detectChanges) {
componentFixture.detectChanges()
}
}

0 comments on commit a8ee8fd

Please sign in to comment.