|
| 1 | +import { patchState, signalStore, type } from '@ngrx/signals'; |
| 2 | +import { createPageArray, gotoPage, setPageSize, withPagination } from './with-pagination'; |
| 3 | +import { setAllEntities, withEntities } from '@ngrx/signals/entities'; |
| 4 | + |
| 5 | +type Book = { id: number; title: string; author: string }; |
| 6 | +const generateBooks = (count = 10) => { |
| 7 | + const books = [] as Book[]; |
| 8 | + for (let i = 1; i <= count; i++) { |
| 9 | + books.push({ id: i, title: `Book ${i}`, author: `Author ${i}` }); |
| 10 | + } |
| 11 | + return books; |
| 12 | +}; |
| 13 | + |
| 14 | +describe('withPagination', () => { |
| 15 | + it('should use and update a pagination', () => { |
| 16 | + const Store = signalStore( |
| 17 | + withEntities({ entity: type<Book>() }), |
| 18 | + withPagination() |
| 19 | + ); |
| 20 | + |
| 21 | + const store = new Store(); |
| 22 | + |
| 23 | + patchState(store, setAllEntities(generateBooks(55))); |
| 24 | + expect(store.currentPage()).toBe(1); |
| 25 | + expect(store.pageCount()).toBe(6); |
| 26 | + }), |
| 27 | + it('should use and update a pagination with collection', () => { |
| 28 | + const Store = signalStore( |
| 29 | + withEntities({ entity: type<Book>(), collection: 'books' }), |
| 30 | + withPagination({ collection: 'books' }) |
| 31 | + ); |
| 32 | + |
| 33 | + const store = new Store(); |
| 34 | + |
| 35 | + patchState( |
| 36 | + store, |
| 37 | + setAllEntities(generateBooks(55), { collection: 'books' }) |
| 38 | + ); |
| 39 | + |
| 40 | + patchState(store, gotoPage(6, { collection: 'books' })); |
| 41 | + expect(store.booksCurrentPage()).toBe(6); |
| 42 | + expect(store.selectedPageBooksEntities().length).toBe(5); |
| 43 | + expect(store.booksPageCount()).toBe(6); |
| 44 | + }), |
| 45 | + it('should react on enitiy changes', () => { |
| 46 | + const Store = signalStore( |
| 47 | + withEntities({ entity: type<Book>()}), |
| 48 | + withPagination() |
| 49 | + ); |
| 50 | + |
| 51 | + const store = new Store(); |
| 52 | + |
| 53 | + patchState( |
| 54 | + store, |
| 55 | + setAllEntities(generateBooks(100)) |
| 56 | + ); |
| 57 | + |
| 58 | + expect(store.pageCount()).toBe(10); |
| 59 | + |
| 60 | + patchState( |
| 61 | + store, |
| 62 | + setAllEntities(generateBooks(20)) |
| 63 | + ); |
| 64 | + |
| 65 | + expect(store.pageCount()).toBe(2); |
| 66 | + |
| 67 | + |
| 68 | + patchState( |
| 69 | + store, |
| 70 | + setPageSize(5) |
| 71 | + ); |
| 72 | + |
| 73 | + expect(store.pageCount()).toBe(4); |
| 74 | + |
| 75 | + }), |
| 76 | + describe('internal pageNavigationArray', () => { |
| 77 | + it('should return an array of page numbers', () => { |
| 78 | + const pages = createPageArray(8, 10, 500, 7); |
| 79 | + expect(pages).toEqual([ |
| 80 | + { label: 5, value: 5 }, |
| 81 | + { label: '...', value: 6 }, |
| 82 | + { label: 7, value: 7 }, |
| 83 | + { label: 8, value: 8 }, |
| 84 | + { label: 9, value: 9 }, |
| 85 | + { label: '...', value: 10 }, |
| 86 | + { label: 50, value: 50 }, |
| 87 | + ]); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments