Skip to content

Commit

Permalink
Add tests setup and example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs authored and Pl217 committed Mar 19, 2024
1 parent 844a58a commit 82c218c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/resolvers/software-info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { version as softwareVersion } from '../../package.json';
import ContextProvider from '../testContext';

const testSoftwareInfo =
(version: boolean, title: boolean, status: boolean) => async () => {
const response =
await ContextProvider.Instance.apolloTestServer.executeOperation({
query: `query { softwareInfo { ${title ? 'title' : ''} ${
status ? 'status' : ''
} ${version ? 'version' : ''} } }`,
});

expect(response).toBeDefined();
expect(response.errors).toBeUndefined();
expect(response.data).toBeDefined();

const data = response.data;
expect(data?.softwareInfo).toBeDefined();
expect(data?.softwareInfo.length).toBeGreaterThan(0);
const softwareInfo = data?.softwareInfo[0];

if (version) {
expect(softwareInfo.version).toBe(softwareVersion);
} else {
expect(softwareInfo.version).toBeUndefined();
}

if (title) {
expect(softwareInfo.title).toBeDefined();
} else {
expect(softwareInfo.title).toBeUndefined();
}

if (status) {
expect(softwareInfo.status).toBeDefined();
} else {
expect(softwareInfo.status).toBeUndefined();
}
};

describe('Query should return Software info', () => {
it('All data should be returned', testSoftwareInfo(true, true, true));

it('Only version should be returned', testSoftwareInfo(true, false, false));

it('Only title should be returned', testSoftwareInfo(false, true, false));

it('Only status should be returned', testSoftwareInfo(false, false, true));
});
5 changes: 5 additions & 0 deletions tests/test-environment-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ContextProvider from './testContext';

beforeAll(async () => {
await ContextProvider.Instance.setUpContext();
});
30 changes: 30 additions & 0 deletions tests/test-environment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ContextProvider from './testContext';

const context = ContextProvider.Instance;

describe('Ensure test environment is setup correctly', () => {
it('should be able to run a test', () => {
expect(true).toBe(true);
});
});

describe('ContextProvider should be defined', () => {
it('should be defined', () => {
expect(context).toBeDefined();
});

it('should have a connection', () => {
expect(context.conn).toBeDefined();
});

it('connection should be a knex connection', () => {
expect(context.conn).toBeDefined();
expect(context.conn?.client.config.client).toEqual('pg');
});

it('knex connection should be connected', async () => {
expect(context.conn).toBeDefined();
const res = await context.conn?.raw('SELECT NOW()');
expect(res.rows[0].now).toBeDefined();
});
});

0 comments on commit 82c218c

Please sign in to comment.