(joinType: JoinType, autoIndex: `off` | `eager`)
| 104 | } as const |
| 105 | |
| 106 | function testJoinType(joinType: JoinType, autoIndex: `off` | `eager`) { |
| 107 | describe(`${joinType} joins with autoIndex ${autoIndex}`, () => { |
| 108 | let usersCollection: ReturnType<typeof createUsersCollection> |
| 109 | let departmentsCollection: ReturnType<typeof createDepartmentsCollection> |
| 110 | |
| 111 | beforeEach(() => { |
| 112 | usersCollection = createUsersCollection(autoIndex) |
| 113 | departmentsCollection = createDepartmentsCollection(autoIndex) |
| 114 | }) |
| 115 | |
| 116 | test(`should perform ${joinType} join with explicit select`, () => { |
| 117 | const joinQuery = createLiveQueryCollection({ |
| 118 | startSync: true, |
| 119 | query: (q) => |
| 120 | q |
| 121 | .from({ user: usersCollection }) |
| 122 | .join( |
| 123 | { dept: departmentsCollection }, |
| 124 | ({ user, dept }) => eq(user.department_id, dept.id), |
| 125 | joinType, |
| 126 | ) |
| 127 | .select(({ user, dept }) => ({ |
| 128 | user_name: user.name, |
| 129 | department_name: dept.name, |
| 130 | budget: dept.budget, |
| 131 | })), |
| 132 | }) |
| 133 | |
| 134 | const results = joinQuery.toArray |
| 135 | const expected = expectedResults[joinType] |
| 136 | |
| 137 | expect(results).toHaveLength(expected.initialCount) |
| 138 | |
| 139 | // Check specific behaviors for each join type |
| 140 | if (joinType === `inner`) { |
| 141 | // Inner join should only include matching records |
| 142 | const userNames = results.map((r) => r.user_name).sort() |
| 143 | expect(userNames).toEqual([`Alice`, `Bob`, `Charlie`]) |
| 144 | |
| 145 | const alice = results.find((r) => r.user_name === `Alice`) |
| 146 | expect(alice).toMatchObject({ |
| 147 | user_name: `Alice`, |
| 148 | department_name: `Engineering`, |
| 149 | budget: 100000, |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | if (joinType === `left`) { |
| 154 | // Left join should include all users, even Dave with null department |
| 155 | const userNames = results.map((r) => r.user_name).sort() |
| 156 | expect(userNames).toEqual([`Alice`, `Bob`, `Charlie`, `Dave`]) |
| 157 | |
| 158 | const dave = results.find((r) => r.user_name === `Dave`) |
| 159 | expect(dave).toMatchObject({ |
| 160 | user_name: `Dave`, |
| 161 | department_name: undefined, |
| 162 | budget: undefined, |
| 163 | }) |
no test coverage detected
searching dependent graphs…