|
| 1 | +import { flushPromises, mount } from "@test/util"; |
| 2 | +import { CListPage } from ".."; |
| 3 | +import { ComplexModelListViewModel } from "@test-targets/viewmodels.g"; |
| 4 | +import { mockEndpoint } from "coalesce-vue/lib/test-utils"; |
| 5 | + |
| 6 | +describe("CListPage", () => { |
| 7 | + test("regular list with pageCount", async () => { |
| 8 | + const list = new ComplexModelListViewModel(); |
| 9 | + |
| 10 | + // Mock the API response with pagination info |
| 11 | + mockEndpoint("/ComplexModel/list", () => ({ |
| 12 | + wasSuccessful: true, |
| 13 | + list: [ |
| 14 | + { complexModelId: 1, name: "Item 1" }, |
| 15 | + { complexModelId: 2, name: "Item 2" }, |
| 16 | + ], |
| 17 | + page: 1, |
| 18 | + pageSize: 10, |
| 19 | + pageCount: 5, |
| 20 | + totalCount: 50, |
| 21 | + })); |
| 22 | + |
| 23 | + // Load the list to populate page count |
| 24 | + await list.$load(); |
| 25 | + |
| 26 | + const wrapper = mount(() => <CListPage list={list} />); |
| 27 | + await flushPromises(); |
| 28 | + |
| 29 | + // Should show "Page X of 5" |
| 30 | + expect(wrapper.text()).toContain("Page"); |
| 31 | + expect(wrapper.text()).toContain("of 5"); |
| 32 | + |
| 33 | + // Should have a page number input |
| 34 | + const input = wrapper.find('input[type="number"]'); |
| 35 | + expect(input.exists()).toBe(true); |
| 36 | + expect((input.element as HTMLInputElement).value).toBe("1"); |
| 37 | + |
| 38 | + // Input should have a max attribute set to the page count |
| 39 | + expect(input.attributes("max")).toBe("5"); |
| 40 | + |
| 41 | + // Previous button should be disabled on first page |
| 42 | + const buttons = wrapper.findAll("button"); |
| 43 | + const prevButton = buttons[0]; |
| 44 | + expect(prevButton.attributes("disabled")).toBeDefined(); |
| 45 | + |
| 46 | + // Next button should be enabled |
| 47 | + const nextButton = buttons[1]; |
| 48 | + expect(nextButton.attributes("disabled")).toBeUndefined(); |
| 49 | + |
| 50 | + // Click next button |
| 51 | + await nextButton.trigger("click"); |
| 52 | + await flushPromises(); |
| 53 | + |
| 54 | + // Page should increment |
| 55 | + expect(list.$page).toBe(2); |
| 56 | + |
| 57 | + // Manually change the input value |
| 58 | + await input.setValue("4"); |
| 59 | + await flushPromises(); |
| 60 | + |
| 61 | + // Page should update to the new value |
| 62 | + expect(list.$page).toBe(4); |
| 63 | + expect((input.element as HTMLInputElement).value).toBe("4"); |
| 64 | + }); |
| 65 | + |
| 66 | + test("list with counting turned off (pageCount = -1)", async () => { |
| 67 | + const list = new ComplexModelListViewModel(); |
| 68 | + // Disable counting by setting noCount to true |
| 69 | + list.$params.noCount = true; |
| 70 | + |
| 71 | + // Mock the API response without page count |
| 72 | + mockEndpoint("/ComplexModel/list", () => ({ |
| 73 | + wasSuccessful: true, |
| 74 | + list: [ |
| 75 | + { complexModelId: 1, name: "Item 1" }, |
| 76 | + { complexModelId: 2, name: "Item 2" }, |
| 77 | + ], |
| 78 | + page: 1, |
| 79 | + pageSize: 10, |
| 80 | + pageCount: -1, |
| 81 | + totalCount: -1, |
| 82 | + })); |
| 83 | + |
| 84 | + // Load the list |
| 85 | + await list.$load(); |
| 86 | + |
| 87 | + const wrapper = mount(() => <CListPage list={list} />); |
| 88 | + await flushPromises(); |
| 89 | + |
| 90 | + // Should show "Page X" without "of N" |
| 91 | + expect(wrapper.text()).toContain("Page"); |
| 92 | + expect(wrapper.text()).not.toContain("of"); |
| 93 | + |
| 94 | + // Should have a page number input |
| 95 | + const input = wrapper.find('input[type="number"]'); |
| 96 | + expect(input.exists()).toBe(true); |
| 97 | + expect((input.element as HTMLInputElement).value).toBe("1"); |
| 98 | + |
| 99 | + // Input should NOT have a max attribute when pageCount is -1 |
| 100 | + expect(input.attributes("max")).toBeUndefined(); |
| 101 | + |
| 102 | + // Previous button should be disabled on first page |
| 103 | + const buttons = wrapper.findAll("button"); |
| 104 | + const prevButton = buttons[0]; |
| 105 | + expect(prevButton.attributes("disabled")).toBeDefined(); |
| 106 | + |
| 107 | + // Next button should be enabled (since we don't know if there's a next page) |
| 108 | + const nextButton = buttons[1]; |
| 109 | + expect(nextButton.attributes("disabled")).toBeUndefined(); |
| 110 | + |
| 111 | + // Click next button |
| 112 | + await nextButton.trigger("click"); |
| 113 | + await flushPromises(); |
| 114 | + |
| 115 | + // Page should increment |
| 116 | + expect(list.$page).toBe(2); |
| 117 | + |
| 118 | + // Manually change the input value |
| 119 | + await input.setValue("10"); |
| 120 | + await flushPromises(); |
| 121 | + |
| 122 | + // Page should update to the new value even without a max limit |
| 123 | + expect(list.$page).toBe(10); |
| 124 | + expect((input.element as HTMLInputElement).value).toBe("10"); |
| 125 | + }); |
| 126 | +}); |
0 commit comments