Skip to content

Commit

Permalink
test: add unit tests to get-orgs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
teresalves committed Jul 29, 2022
1 parent 3981726 commit 12822ce
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ router.get('/', async (ctx) => {
router.post('/organisations', async (ctx) => {
const organisationInsertor = new OrganisationInsertor();
const result = await organisationInsertor.processOrganizations(ctx);
ctx.body = result.msg;
ctx.status = result.status;
ctx.body = result.msg;
});

router.get('/organisations/:orgName', async (ctx) => {
const result = await getOrganisation(ctx);
ctx.body = result.body;
ctx.status = result.status;
ctx.body = result.body;
});
3 changes: 1 addition & 2 deletions src/endpoints/get-organisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export async function getOrganisation(ctx: ParameterizedContext) {
'SELECT org_name FROM organisations WHERE org_name=$1;',
[name],
);

if (existanceCheck.rows?.length === 0) {
return { body: 'Organisation does not exist', status: 404 };
}
Expand All @@ -25,7 +24,7 @@ export async function getOrganisation(ctx: ParameterizedContext) {
client.release();

if (result.rows?.length === 0) {
return { body: '', status: 204 };
return { body: undefined, status: 204 };
}
return { body: result.rows, status: 200 };
}
65 changes: 65 additions & 0 deletions test/get-organisation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { pool } from '../src/postgres-setup';
import { request } from './utils';

jest.mock('../src/postgres-setup.ts', () => ({
pool: {
connect: jest.fn().mockResolvedValue({
query: jest.fn(),
release: jest.fn(),
}),
},
}));

const orgValidatorResponse = {
rows: [
{
org_name: 'Paradise Island',
},
],
};

const queryResponse = {
rows: [
{
org_name: 'Banana Tree',
relatioship_type: 'daughter',
},
{
org_name: 'Big Banana Tree',
relationship_type: 'daughter',
},
],
};

describe('get-organisation', () => {
test.only('should get organization responses for an existing org', async () => {
const client = await pool.connect();
(client.query as jest.Mock).mockResolvedValueOnce(orgValidatorResponse);
(client.query as jest.Mock).mockResolvedValueOnce(queryResponse);
const result = await request.get('/organisations/Paradise Island');
expect(result.status).toBe(200);
expect(JSON.stringify(result.body)).toMatch(
JSON.stringify(queryResponse.rows),
);
});

test('should not get organization responses for a non existing org', async () => {
const client = await pool.connect();
(client.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
(client.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
const result = await request.get('/organisations/Paradise Island');
expect(result.status).toBe(404);
// expect(result.body).toMatch("Organisation does not exist")
});

test('should not get organization responses for an org without relatives', async () => {
const client = await pool.connect();
(client.query as jest.Mock).mockResolvedValueOnce({
rows: [{ org_name: 'Paradise Island' }],
});
(client.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
const result = await request.get('/organisations/Paradise Island');
expect(result.status).toBe(204);
// expect(result.body).toBeUndefined();
});
});
17 changes: 15 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { pool } from '../src/postgres-setup';
import { request } from './utils';

jest.mock('../src/postgres-setup.ts', () => ({
pool: {
connect: jest.fn().mockResolvedValue({
query: jest.fn(),
release: jest.fn(),
}),
},
}));

describe('App test', () => {
test('Should request base route with success', async () => {
const client = await pool.connect();
(client.query as jest.Mock).mockResolvedValueOnce({
rows: [{ org_name: 'example' }],
});
const result = await request.get('/');

expect(result.status).toBe(200);
expect(result.text).toBe('Hello World');
expect(result.body).toStrictEqual([{ org_name: 'example' }]);
});
});

0 comments on commit 12822ce

Please sign in to comment.