|
35 | 35 | */ |
36 | 36 | import React from 'react'; |
37 | 37 | import { describe, it, expect, vi, afterEach } from 'vitest'; |
38 | | -import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react'; |
| 38 | +import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
39 | 39 | import '@testing-library/jest-dom'; |
40 | 40 | import { ValueDataSource } from '@object-ui/core'; |
41 | 41 | import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
@@ -672,6 +672,189 @@ describe('a lookup-laned board under a list view counts and totals each whole la |
672 | 672 | expect(screen.queryByRole('group', { name: 'Uncategorized' })).toBeNull(); |
673 | 673 | }); |
674 | 674 |
|
| 675 | + /** |
| 676 | + * objectstack#37517 — a lane that draws fewer cards than the lane read counts |
| 677 | + * loads the rest in place: "Load more" at its foot reads the lane's next |
| 678 | + * records under the view's query (its filter AND the lane value, its order, |
| 679 | + * its search), from right after the lane's cards on the page. |
| 680 | + */ |
| 681 | + const loadMore = (title: string) => within(lane(title)).queryByRole('button', { name: 'Load more' }); |
| 682 | + /** The lane reads "Load more" sent: every `crm_deal` read that names a `$skip`. */ |
| 683 | + const laneReads = (ds: any) => |
| 684 | + ds.find.mock.calls.filter((call: any[]) => call[0] === DEAL && call[1] && '$skip' in call[1]).map((call: any[]) => call[1]); |
| 685 | + const hostPageRead = (ds: any) => |
| 686 | + ds.find.mock.calls.find((call: any[]) => call[0] === DEAL && call[1] && !('$skip' in call[1]))![1]; |
| 687 | + const cardNames = (title: string) => within(lane(title)).queryAllByRole('listitem').map((card) => card.textContent); |
| 688 | + |
| 689 | + it('a lane past the page loads its 10 cards in place, from its first record, and stops offering more', async () => { |
| 690 | + const ds = dealSource(); |
| 691 | + mountBoard(ds, BOARD_VIEW); |
| 692 | + |
| 693 | + await onePageDrawn(); |
| 694 | + await waitFor(() => expect(loadMore('Cyan Corp')).not.toBeNull()); |
| 695 | + // The lane still says what it has not loaded while it offers to load it. |
| 696 | + expect(within(lane('Cyan Corp')).getByText('Cards not loaded: 10')).toBeInTheDocument(); |
| 697 | + expect(laneReads(ds)).toHaveLength(0); |
| 698 | + |
| 699 | + fireEvent.click(loadMore('Cyan Corp')!); |
| 700 | + |
| 701 | + // Drawn from its cards now, the lane is titled as the page's lanes are: by |
| 702 | + // the record its cards reference, which this source does not expand, so by |
| 703 | + // its key (`acc-c`), and in the same place. |
| 704 | + await waitFor(() => expect(within(lane('acc-c')).getAllByRole('listitem')).toHaveLength(10)); |
| 705 | + expect(laneNames().slice(-3)).toEqual(['acc-a', 'acc-b', 'acc-c']); |
| 706 | + expect(loadMore('acc-c')).toBeNull(); |
| 707 | + expect(laneBadge('acc-c')).toBe('10'); |
| 708 | + expect(within(lane('acc-c')).queryByText('Cards not loaded: 10')).toBeNull(); |
| 709 | + // The other lanes keep their page's cards. |
| 710 | + expect(within(lane('acc-a')).getAllByRole('listitem')).toHaveLength(13); |
| 711 | + expect(cards()).toHaveLength(35); |
| 712 | + const reads = laneReads(ds); |
| 713 | + expect(reads).toHaveLength(1); |
| 714 | + expect(reads[0]).toMatchObject({ $skip: 0, $top: 50, $count: true }); |
| 715 | + expect(JSON.stringify(reads[0].$filter)).toContain('"account","=","acc-c"'); |
| 716 | + expect(reads[0].$orderby).toEqual(hostPageRead(ds).$orderby); |
| 717 | + }); |
| 718 | + |
| 719 | + it('a lane on the page with 13 of its 30 cards loads the other 17 from its 14th record, none twice', async () => { |
| 720 | + const ds = dealSource(); |
| 721 | + mountBoard(ds, BOARD_VIEW); |
| 722 | + |
| 723 | + await onePageDrawn(); |
| 724 | + await waitFor(() => expect(loadMore('acc-a')).not.toBeNull()); |
| 725 | + expect(loadMore('acc-b')).not.toBeNull(); |
| 726 | + |
| 727 | + fireEvent.click(loadMore('acc-a')!); |
| 728 | + |
| 729 | + await waitFor(() => expect(within(lane('acc-a')).getAllByRole('listitem')).toHaveLength(30)); |
| 730 | + expect(new Set(cardNames('acc-a')).size).toBe(30); |
| 731 | + expect(loadMore('acc-a')).toBeNull(); |
| 732 | + expect(laneBadge('acc-a')).toBe('30'); |
| 733 | + // acc-b is untouched: its page's 12 and its "Load more". |
| 734 | + expect(within(lane('acc-b')).getAllByRole('listitem')).toHaveLength(12); |
| 735 | + expect(loadMore('acc-b')).not.toBeNull(); |
| 736 | + const reads = laneReads(ds); |
| 737 | + expect(reads).toHaveLength(1); |
| 738 | + expect(reads[0]).toMatchObject({ $skip: 13, $top: 50 }); |
| 739 | + expect(JSON.stringify(reads[0].$filter)).toContain('"account","=","acc-a"'); |
| 740 | + }); |
| 741 | + |
| 742 | + it('a page that repeats a card the lane already holds draws it once', async () => { |
| 743 | + // A source whose lane read starts two records early, as a sort with ties |
| 744 | + // may answer: deals 22 and 24 come back a second time. |
| 745 | + const ds = dealSource(); |
| 746 | + const find = ds.find.getMockImplementation(); |
| 747 | + ds.find.mockImplementation((object: string, params: any) => |
| 748 | + find(object, params && '$skip' in params ? { ...params, $skip: Math.max(0, params.$skip - 2) } : params)); |
| 749 | + mountBoard(ds, BOARD_VIEW); |
| 750 | + |
| 751 | + await onePageDrawn(); |
| 752 | + await waitFor(() => expect(loadMore('acc-a')).not.toBeNull()); |
| 753 | + fireEvent.click(loadMore('acc-a')!); |
| 754 | + |
| 755 | + await waitFor(() => expect(laneReads(ds)).toHaveLength(1)); |
| 756 | + await waitFor(() => expect(within(lane('acc-a')).getAllByRole('listitem')).toHaveLength(30)); |
| 757 | + expect(new Set(cardNames('acc-a')).size).toBe(30); |
| 758 | + expect(laneBadge('acc-a')).toBe('30'); |
| 759 | + }); |
| 760 | + |
| 761 | + it('reads under the view\'s filter, the one its own window was fetched with', async () => { |
| 762 | + const ds = dealSource(); |
| 763 | + mountBoard(ds, { ...BOARD_VIEW, filter: [{ field: 'amount', operator: 'greater_than', value: 9 }] }); |
| 764 | + |
| 765 | + await waitFor(() => expect(cards()).toHaveLength(25)); |
| 766 | + await waitFor(() => expect(loadMore('acc-a')).not.toBeNull()); |
| 767 | + fireEvent.click(loadMore('acc-a')!); |
| 768 | + |
| 769 | + // 10, 12, … 58: 25 records, 13 of them on the page. |
| 770 | + await waitFor(() => expect(within(lane('acc-a')).getAllByRole('listitem')).toHaveLength(25)); |
| 771 | + expect(loadMore('acc-a')).toBeNull(); |
| 772 | + const read = laneReads(ds)[0]; |
| 773 | + expect(read.$skip).toBe(13); |
| 774 | + expect(JSON.stringify(read.$filter)).toContain('amount'); |
| 775 | + expect(JSON.stringify(read.$filter)).toContain('"account","=","acc-a"'); |
| 776 | + }); |
| 777 | + |
| 778 | + it('the "Uncategorized" lane past the page loads its records with no account', async () => { |
| 779 | + const ds = dealSource(true, NO_ACCOUNT_PAST_PAGE); |
| 780 | + mountBoard(ds, BOARD_VIEW); |
| 781 | + |
| 782 | + await onePageDrawn(); |
| 783 | + await waitFor(() => expect(loadMore('Uncategorized')).not.toBeNull()); |
| 784 | + expect(within(lane('Uncategorized')).getByText('Cards not loaded: 10')).toBeInTheDocument(); |
| 785 | + fireEvent.click(loadMore('Uncategorized')!); |
| 786 | + |
| 787 | + await waitFor(() => expect(within(lane('Uncategorized')).getAllByRole('listitem')).toHaveLength(10)); |
| 788 | + expect(loadMore('Uncategorized')).toBeNull(); |
| 789 | + expect(laneBadge('Uncategorized')).toBe('10'); |
| 790 | + const read = laneReads(ds)[0]; |
| 791 | + expect(read.$skip).toBe(0); |
| 792 | + expect(JSON.stringify(read.$filter)).toContain('"account","is_null"'); |
| 793 | + }); |
| 794 | + |
| 795 | + it('the cards a lane loaded go when the list reads its page again (Refresh), and the lane offers them again', async () => { |
| 796 | + const ds = dealSource(); |
| 797 | + mountBoard(ds, { ...BOARD_VIEW, compactToolbar: false }); |
| 798 | + |
| 799 | + await onePageDrawn(); |
| 800 | + await waitFor(() => expect(loadMore('Cyan Corp')).not.toBeNull()); |
| 801 | + fireEvent.click(loadMore('Cyan Corp')!); |
| 802 | + await waitFor(() => expect(within(lane('acc-c')).getAllByRole('listitem')).toHaveLength(10)); |
| 803 | + |
| 804 | + fireEvent.click(screen.getByTestId('refresh-button')); |
| 805 | + |
| 806 | + await waitFor(() => expect(within(lane('Cyan Corp')).queryAllByRole('listitem')).toHaveLength(0)); |
| 807 | + await waitFor(() => expect(loadMore('Cyan Corp')).not.toBeNull()); |
| 808 | + expect(within(lane('Cyan Corp')).getByText('Cards not loaded: 10')).toBeInTheDocument(); |
| 809 | + expect(cards()).toHaveLength(25); |
| 810 | + }); |
| 811 | + |
| 812 | + it('the cards a lane loaded go when the toolbar search changes, and the next read carries the search', async () => { |
| 813 | + const ds = dealSource(); |
| 814 | + mountBoard(ds, { ...BOARD_VIEW, compactToolbar: false }); |
| 815 | + |
| 816 | + await onePageDrawn(); |
| 817 | + await waitFor(() => expect(loadMore('Cyan Corp')).not.toBeNull()); |
| 818 | + fireEvent.click(loadMore('Cyan Corp')!); |
| 819 | + await waitFor(() => expect(within(lane('acc-c')).getAllByRole('listitem')).toHaveLength(10)); |
| 820 | + |
| 821 | + // Every deal is named "Deal …", so the search keeps every record. |
| 822 | + fireEvent.click(screen.getByTestId('search-icon-button')); |
| 823 | + fireEvent.change(screen.getByPlaceholderText(/search/i), { target: { value: 'Deal' } }); |
| 824 | + |
| 825 | + await waitFor(() => expect(within(lane('Cyan Corp')).queryAllByRole('listitem')).toHaveLength(0)); |
| 826 | + await waitFor(() => expect(loadMore('Cyan Corp')).not.toBeNull()); |
| 827 | + fireEvent.click(loadMore('Cyan Corp')!); |
| 828 | + await waitFor(() => expect(within(lane('acc-c')).getAllByRole('listitem')).toHaveLength(10)); |
| 829 | + const reads = laneReads(ds); |
| 830 | + expect(reads).toHaveLength(2); |
| 831 | + expect(reads[0].$search).toBeUndefined(); |
| 832 | + expect(reads[1]).toMatchObject({ $search: 'Deal', $skip: 0 }); |
| 833 | + }); |
| 834 | + |
| 835 | + it.each([ |
| 836 | + ['has no grouped door', false], |
| 837 | + ['refuses the read', 'refuse'], |
| 838 | + ['cuts the read (hasMore)', 'cut'], |
| 839 | + ] as const)('CONTROL: when the source %s, no lane offers "Load more"', async (_, door) => { |
| 840 | + const ds = dealSource(door, NO_ACCOUNT_PAST_PAGE); |
| 841 | + mountBoard(ds, BOARD_VIEW); |
| 842 | + |
| 843 | + await onePageDrawn(); |
| 844 | + if (door !== false) await waitFor(() => expect(ds.queryGroupHeaders).toHaveBeenCalledTimes(1)); |
| 845 | + await waitFor(() => expect(laneBadge('acc-a')).toBe('13+')); |
| 846 | + expect(screen.queryAllByRole('button', { name: 'Load more' })).toHaveLength(0); |
| 847 | + expect(laneReads(ds)).toHaveLength(0); |
| 848 | + }); |
| 849 | + |
| 850 | + it('CONTROL: a board laned by a text field offers no "Load more"', async () => { |
| 851 | + mountBoard(dealSource(), { ...BOARD_VIEW, kanban: { ...BOARD_VIEW.kanban, groupByField: 'tier' } }); |
| 852 | + |
| 853 | + await waitFor(() => expect(cards()).toHaveLength(25)); |
| 854 | + await waitFor(() => expect(laneBadge('gold')).toBe('30')); |
| 855 | + expect(screen.queryAllByRole('button', { name: 'Load more' })).toHaveLength(0); |
| 856 | + }); |
| 857 | + |
675 | 858 | it('a board laned by a text field counts each whole lane but draws no lane past the page', async () => { |
676 | 859 | const ds = dealSource(); |
677 | 860 | mountBoard(ds, { ...BOARD_VIEW, kanban: { ...BOARD_VIEW.kanban, groupByField: 'tier' } }); |
|
0 commit comments