|
| 1 | +import { Component } from '@angular/core' |
| 2 | +import { render } from '@testing-library/angular' |
| 3 | +import '@testing-library/jest-dom' |
| 4 | +import { configureStore, createSlice } from '@reduxjs/toolkit' |
| 5 | +import { provideRedux, injectDispatch, injectSelector } from '../public-api' |
| 6 | +import { userEvent } from '@testing-library/user-event' |
| 7 | + |
| 8 | +const user = userEvent.setup(); |
| 9 | + |
| 10 | +const counterSlice = createSlice({ |
| 11 | + name: 'counter', |
| 12 | + initialState: { |
| 13 | + value: 0, |
| 14 | + }, |
| 15 | + reducers: { |
| 16 | + increment: (state) => { |
| 17 | + state.value += 1 |
| 18 | + } |
| 19 | + }, |
| 20 | +}) |
| 21 | + |
| 22 | +@Component({ |
| 23 | + selector: 'app-root', |
| 24 | + standalone: true, |
| 25 | + template: ` |
| 26 | + <div> |
| 27 | + <div> |
| 28 | + <button |
| 29 | + aria-label="Increment value" |
| 30 | + (click)="dispatch(increment())" |
| 31 | + > |
| 32 | + Increment |
| 33 | + </button> |
| 34 | + <span>Count: {{ count() }}</span> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + ` |
| 38 | +}) |
| 39 | +export class AppComponent { |
| 40 | + count = injectSelector((state: any) => state.counter.value) |
| 41 | + dispatch = injectDispatch() |
| 42 | + increment = counterSlice.actions.increment |
| 43 | +} |
| 44 | + |
| 45 | +it('injectSelector should work without reactivity', async () => { |
| 46 | + const store = configureStore({ |
| 47 | + reducer: { |
| 48 | + counter: counterSlice.reducer, |
| 49 | + }, |
| 50 | + }) |
| 51 | + |
| 52 | + const {getByText} = await render(AppComponent, { |
| 53 | + providers: [provideRedux({store})] |
| 54 | + }) |
| 55 | + |
| 56 | + expect(getByText("Count: 0")).toBeInTheDocument(); |
| 57 | +}) |
| 58 | + |
| 59 | +it('injectSelector should work with reactivity', async () => { |
| 60 | + const store = configureStore({ |
| 61 | + reducer: { |
| 62 | + counter: counterSlice.reducer, |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + const {getByText, getByLabelText} = await render(AppComponent, { |
| 67 | + providers: [provideRedux({store})] |
| 68 | + }) |
| 69 | + |
| 70 | + expect(getByText("Count: 0")).toBeInTheDocument(); |
| 71 | + |
| 72 | + await user.click(getByLabelText("Increment value")) |
| 73 | + |
| 74 | + expect(getByText("Count: 1")).toBeInTheDocument(); |
| 75 | +}) |
0 commit comments