|
| 1 | +import {InjectSelector, injectSelector, provideRedux} from '../public-api' |
| 2 | +import {Component} from "@angular/core"; |
| 3 | +import {render} from "@testing-library/angular"; |
| 4 | +import {AnyAction, createStore, Store} from "redux"; |
| 5 | +import '@testing-library/jest-dom'; |
| 6 | + |
| 7 | +type NormalStateType = { |
| 8 | + count: number |
| 9 | +} |
| 10 | +let normalStore: Store<NormalStateType, AnyAction> |
| 11 | +let renderedItems: any[] = [] |
| 12 | +type RootState = ReturnType<typeof normalStore.getState> |
| 13 | +const injectNormalSelector: InjectSelector<RootState> = injectSelector |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + normalStore = createStore( |
| 17 | + ({count}: NormalStateType = {count: -1}): NormalStateType => ({ |
| 18 | + count: count + 1, |
| 19 | + }), |
| 20 | + ) |
| 21 | + renderedItems = [] |
| 22 | +}) |
| 23 | + |
| 24 | +describe('injectSelector core subscription behavior', () => { |
| 25 | + it('selects the state on initial render', async () => { |
| 26 | + @Component({ |
| 27 | + selector: "app-root", |
| 28 | + standalone: true, |
| 29 | + template: "<div>Count: {{count()}}</div>" |
| 30 | + }) |
| 31 | + class Testing { |
| 32 | + count = injectNormalSelector((state) => state.count) |
| 33 | + } |
| 34 | + |
| 35 | + const {getByText} = await render(Testing, { |
| 36 | + providers: [provideRedux({store: normalStore})] |
| 37 | + }) |
| 38 | + |
| 39 | + expect(getByText("Count: 0")).toBeInTheDocument(); |
| 40 | + }) |
| 41 | + |
| 42 | + it('selects the state and renders the component when the store updates', async () => { |
| 43 | + const selector = jest.fn((s: NormalStateType) => s.count) |
| 44 | + |
| 45 | + @Component({ |
| 46 | + selector: "app-root", |
| 47 | + standalone: true, |
| 48 | + template: "<div>Count: {{count()}}</div>" |
| 49 | + }) |
| 50 | + class Testing { |
| 51 | + count = injectNormalSelector(selector) |
| 52 | + } |
| 53 | + |
| 54 | + const {findByText} = await render(Testing, { |
| 55 | + providers: [provideRedux({store: normalStore})] |
| 56 | + }) |
| 57 | + |
| 58 | + expect(await findByText("Count: 0")).toBeInTheDocument(); |
| 59 | + expect(selector).toHaveBeenCalledTimes(1) |
| 60 | + |
| 61 | + normalStore.dispatch({ type: '' }) |
| 62 | + |
| 63 | + |
| 64 | + expect(await findByText("Count: 1")).toBeInTheDocument(); |
| 65 | + expect(selector).toHaveBeenCalledTimes(2) |
| 66 | + }) |
| 67 | +}) |
0 commit comments