|
1 |
| -import { EntityStateAdapter, EntityState } from './models' |
| 1 | +import { EntityStateAdapter, EntityState, EntityAdapter } from './models' |
2 | 2 | import { createEntityAdapter } from './create_adapter'
|
3 | 3 | import { createAction } from '../createAction'
|
4 | 4 | import {
|
@@ -753,4 +753,75 @@ describe('Sorted State Adapter', () => {
|
753 | 753 | `)
|
754 | 754 | })
|
755 | 755 | })
|
| 756 | + |
| 757 | + describe('Sorted adapter indices', () => { |
| 758 | + const booksAdapter = createEntityAdapter<BookModel>({ |
| 759 | + selectId: (book: BookModel) => book.id, |
| 760 | + sortComparer: (a, b) => { |
| 761 | + return a.title.localeCompare(b.title) |
| 762 | + }, |
| 763 | + indices: { |
| 764 | + byId: (a, b) => a.id.localeCompare(b.id), |
| 765 | + byTitleLength: (a, b) => a.title.length - b.title.length |
| 766 | + } |
| 767 | + }) |
| 768 | + |
| 769 | + const initialState = booksAdapter.getInitialState() |
| 770 | + beforeEach(() => {}) |
| 771 | + |
| 772 | + it('Does some initial sorting', () => { |
| 773 | + const loadedState = booksAdapter.setAll(initialState, [ |
| 774 | + TheGreatGatsby, |
| 775 | + AClockworkOrange, |
| 776 | + AnimalFarm |
| 777 | + ]) |
| 778 | + expect(loadedState.indices.byId.length).toBe(3) |
| 779 | + }) |
| 780 | + |
| 781 | + it('Resorts only updated indices', () => { |
| 782 | + const loadedState = booksAdapter.setAll(initialState, [ |
| 783 | + TheGreatGatsby, |
| 784 | + AClockworkOrange, |
| 785 | + AnimalFarm |
| 786 | + ]) |
| 787 | + |
| 788 | + const updatedState = booksAdapter.updateOne(loadedState, { |
| 789 | + id: AnimalFarm.id, |
| 790 | + changes: { |
| 791 | + title: 'This is a really long title, very long indeed' |
| 792 | + } |
| 793 | + }) |
| 794 | + |
| 795 | + expect(updatedState.indices.byTitleLength[2]).toBe(AnimalFarm.id) |
| 796 | + expect(updatedState.indices.byId).toBe(loadedState.indices.byId) |
| 797 | + }) |
| 798 | + |
| 799 | + it('Updates indices when items are removed', () => { |
| 800 | + const loadedState = booksAdapter.setAll(initialState, [ |
| 801 | + TheGreatGatsby, |
| 802 | + AClockworkOrange, |
| 803 | + AnimalFarm |
| 804 | + ]) |
| 805 | + |
| 806 | + const updatedState1 = booksAdapter.removeOne( |
| 807 | + loadedState, |
| 808 | + AClockworkOrange.id |
| 809 | + ) |
| 810 | + |
| 811 | + expect(updatedState1.indices.byId.length).toBe(2) |
| 812 | + expect(updatedState1.indices.byTitleLength.length).toBe(2) |
| 813 | + |
| 814 | + const updatedState2 = booksAdapter.removeMany(loadedState, [ |
| 815 | + AClockworkOrange.id |
| 816 | + ]) |
| 817 | + |
| 818 | + expect(updatedState2.indices.byId.length).toBe(2) |
| 819 | + expect(updatedState2.indices.byTitleLength.length).toBe(2) |
| 820 | + |
| 821 | + const updatedState3 = booksAdapter.removeAll(loadedState) |
| 822 | + |
| 823 | + expect(updatedState3.indices.byId.length).toBe(0) |
| 824 | + expect(updatedState3.indices.byTitleLength.length).toBe(0) |
| 825 | + }) |
| 826 | + }) |
756 | 827 | })
|
0 commit comments