Skip to content

Commit

Permalink
Improve support for first & pluck methods, closes #3 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh authored Aug 17, 2021
1 parent bd20cf1 commit 5eb5fcd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/MockClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MockClient extends knex.Client {
switch (rawMethod) {
case 'first':
case 'pluck':
rawQuery.postOp = rawMethod;
method = 'select';
break;
case 'del':
Expand Down
11 changes: 10 additions & 1 deletion src/Tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Tracker {
} else {
const data =
typeof handler.data === 'function' ? await handler.data(rawQuery) : handler.data;
resolve(cloneDeep(data));
resolve(cloneDeep(Tracker.applyPostOp(data, rawQuery)));
}

if (handler.once) {
Expand Down Expand Up @@ -135,4 +135,13 @@ export class Tracker {
};
};
}

private static applyPostOp(data: any, rawQuery: RawQuery) {
if (rawQuery.postOp === 'first' && Array.isArray(data)) {
return data[0];
} else if (rawQuery.postOp === 'pluck' && rawQuery.pluck && Array.isArray(data)) {
return data.map((item) => item[rawQuery.pluck as string]);
}
return data;
}
}
24 changes: 19 additions & 5 deletions tests/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,33 @@ describe('mock Select statement', () => {
});

it('should support `first` query', async () => {
tracker.on.select('table_name').response([]);
const data = [
{ id: faker.datatype.number({ min: 1 }) },
{ id: faker.datatype.number({ min: 1 }) },
];

await db('table_name').first();
tracker.on.select('table_name').response(data);

expect(tracker.history.select).toHaveLength(1);
const firstValue = await db('table_name').first();
const allValues = await db('table_name');

expect(tracker.history.select).toHaveLength(2);
expect(firstValue).toEqual(data[0]);
expect(allValues).toEqual(data);
});

it('should support `pluck` query', async () => {
tracker.on.select('table_name').response([]);
const data = [
{ id: faker.datatype.number({ min: 1 }) },
{ id: faker.datatype.number({ min: 1 }) },
];

tracker.on.select('table_name').response(data);

await db('table_name').pluck('id');
const values = await db('table_name').pluck('id');

expect(tracker.history.select).toHaveLength(1);
expect(values).toEqual(data.map(({ id }) => id));
});

it('should support `raw` select statement', async () => {
Expand Down
2 changes: 2 additions & 0 deletions types/mock-client.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export interface RawQuery {
method: 'select' | 'insert' | 'update' | 'delete' | 'any';
sql: string;
postOp?: 'first' | 'pluck';
pluck?: string;
bindings: any[];
options: Record<string, any>;
timeout: boolean;
Expand Down

0 comments on commit 5eb5fcd

Please sign in to comment.