(autoIndex: `off` | `eager`)
| 122 | } |
| 123 | |
| 124 | function createDistinctTests(autoIndex: `off` | `eager`): void { |
| 125 | describe(`with autoIndex ${autoIndex}`, () => { |
| 126 | describe(`Basic Usage`, () => { |
| 127 | let usersCollection: ReturnType<typeof createUsersCollection> |
| 128 | |
| 129 | beforeEach(() => { |
| 130 | usersCollection = createUsersCollection(autoIndex) |
| 131 | }) |
| 132 | |
| 133 | test(`distinct on a single column`, () => { |
| 134 | const distinctCountries = createLiveQueryCollection({ |
| 135 | startSync: true, |
| 136 | query: (q) => |
| 137 | q |
| 138 | .from({ users: usersCollection }) |
| 139 | .select(({ users }) => ({ country: users.country })) |
| 140 | .distinct(), |
| 141 | }) |
| 142 | |
| 143 | expect(distinctCountries.size).toBe(3) // USA, Canada, UK |
| 144 | |
| 145 | const countries = Array.from(distinctCountries.values()).map( |
| 146 | (user) => user.country, |
| 147 | ) |
| 148 | expect(countries).toContain(`USA`) |
| 149 | expect(countries).toContain(`Canada`) |
| 150 | expect(countries).toContain(`UK`) |
| 151 | expect(countries.length).toBe(3) |
| 152 | }) |
| 153 | |
| 154 | test(`distinct on multiple columns`, () => { |
| 155 | const distinctRoleSalary = createLiveQueryCollection({ |
| 156 | startSync: true, |
| 157 | query: (q) => |
| 158 | q |
| 159 | .from({ users: usersCollection }) |
| 160 | .select(({ users }) => ({ |
| 161 | role: users.role, |
| 162 | salary: users.salary, |
| 163 | })) |
| 164 | .distinct(), |
| 165 | }) |
| 166 | |
| 167 | // Expected unique combinations: |
| 168 | // Developer-75000 (John, Alice, Diana, Frank) |
| 169 | // Developer-80000 (Jane) |
| 170 | // Manager-90000 (Bob, Eve) |
| 171 | // Representative-60000 (Charlie) |
| 172 | expect(distinctRoleSalary.size).toBe(4) |
| 173 | |
| 174 | const combinations = Array.from(distinctRoleSalary.values()).map( |
| 175 | (user) => `${user.role}-${user.salary}`, |
| 176 | ) |
| 177 | expect(combinations).toContain(`Developer-75000`) |
| 178 | expect(combinations).toContain(`Developer-80000`) |
| 179 | expect(combinations).toContain(`Manager-90000`) |
| 180 | expect(combinations).toContain(`Representative-60000`) |
| 181 | expect(combinations.length).toBe(4) |
no test coverage detected