Skip to content

Commit

Permalink
Add url parameter to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemonteith committed Mar 23, 2020
1 parent 30afdea commit a19c89a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
12 changes: 10 additions & 2 deletions lib/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ describe('database', () => {
});

it('inserts true answer into database', async () => {
await database.saveInitialResponse({ isSatisfied: true });
await database.saveInitialResponse({
isSatisfied: true,
url: 'https://example.com/my-test-page/',
});
const data = await db.collection('feedback').find({}).toArray();
expect(data.length).toBe(1);
expect(data[0].isSatisfied).toBe(true);
expect(data[0].url).toBe('https://example.com/my-test-page/');
});

it('inserts false answer into database', async () => {
await database.saveInitialResponse({ isSatisfied: false });
await database.saveInitialResponse({
isSatisfied: false,
url: 'https://example.com/my-test-page/',
});
const data = await db.collection('feedback').find({}).toArray();
expect(data.length).toBe(1);
expect(data[0].isSatisfied).toBe(false);
expect(data[0].url).toBe('https://example.com/my-test-page/');
});

it('inserts text comment into database', async () => {
Expand Down
22 changes: 18 additions & 4 deletions lib/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,44 @@ const validation = require('./validation.js');

describe('validate initial response', () => {
it('when isSatisfied is true', () => {
const dirtyData = { isSatisfied: true };
const dirtyData = { isSatisfied: true, url: 'https://example.com/' };
const cleanData = validation.validateInitialResponse(dirtyData);
expect(cleanData).toEqual(dirtyData);
});

it('when isSatisfied is false', () => {
const dirtyData = { isSatisfied: false };
const dirtyData = { isSatisfied: false, url: 'https://example.com/' };
const cleanData = validation.validateInitialResponse(dirtyData);
expect(cleanData).toEqual(dirtyData);
});

it('when isSatisfied is invalid', () => {
const dirtyData = { isSatisfied: null };
const dirtyData = { isSatisfied: null, url: 'https://example.com/' };
expect(() => {
validation.validateInitialResponse(dirtyData);
}).toThrow('isSatisfied must be true or false');
});

it('when isSatisfied is missing', () => {
const dirtyData = {};
const dirtyData = { url: 'https://example.com/' };
expect(() => {
validation.validateInitialResponse(dirtyData);
}).toThrow('isSatisfied must be true or false');
});

it('when url is missing', () => {
const dirtyData = { isSatisfied: true };
expect(() => {
validation.validateInitialResponse(dirtyData);
}).toThrow('url must be a valid URL');
});

it('when url is not a valid URL', () => {
const dirtyData = { isSatisfied: true, url: 'bobloblawlawblog' };
expect(() => {
validation.validateInitialResponse(dirtyData);
}).toThrow('url must be a valid URL');
});
});

describe('validate text comments', () => {
Expand Down
46 changes: 42 additions & 4 deletions tests/integration/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const UUID_REGEX = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-

describe('API endpoint /satisfied/', () => {
it('returns status ok response', async () => {
const payload = { isSatisfied: true };
const payload = {
isSatisfied: true,
url: 'https://example.com/my-test-page/',
};
const response = await axios.post('http://localhost:7071/satisfied/', payload);
expect(response.status).toEqual(200);
expect(response.data).toEqual(
Expand All @@ -40,22 +43,29 @@ describe('API endpoint /satisfied/', () => {
});

it('saves into the database', async () => {
const payload = { isSatisfied: true };
const payload = {
isSatisfied: true,
url: 'https://example.com/my-test-page/',
};
await axios.post('http://localhost:7071/satisfied/', payload);
const data = await db.collection('feedback').find({}).toArray();
expect(data.length).toBe(1);
expect(data[0]).toEqual(
expect.objectContaining({
isSatisfied: true,
token: expect.stringMatching(UUID_REGEX),
url: 'https://example.com/my-test-page/',
})
);
});
});

describe('API endpoint /comments/', () => {
const getToken = async () => {
const payload = { isSatisfied: true };
const payload = {
isSatisfied: true,
url: 'https://example.com/my-test-page/',
};
const response = await axios.post('http://localhost:7071/satisfied/', payload);
return response.data.token;
};
Expand Down Expand Up @@ -95,7 +105,34 @@ describe('API endpoint /comments/', () => {
});

it('is associated with satisfaction response via token', async () => {
const satisfiedPayload = { isSatisfied: false };
const satisfiedPayload = {
isSatisfied: false,
url: 'https://example.com/my-test-page/',
};
const response = await axios.post('http://localhost:7071/satisfied/', satisfiedPayload);

const payload = {
comments: 'Could do with a bit more cowbell.',
token: response.data.token,
};
await axios.post('http://localhost:7071/comments/', payload);
const data = await db.collection('feedback').find({}).toArray();
expect(data.length).toBe(1);

// comment data should be associated with the earlier "satisfied" API call
expect(data[0]).toEqual(
expect.objectContaining({
comments: 'Could do with a bit more cowbell.',
isSatisfied: false,
})
);
});

it('is associated with url from satisfaction request', async () => {
const satisfiedPayload = {
isSatisfied: false,
url: 'https://example.com/my-test-page/',
};
const response = await axios.post('http://localhost:7071/satisfied/', satisfiedPayload);

const payload = {
Expand All @@ -111,6 +148,7 @@ describe('API endpoint /comments/', () => {
expect.objectContaining({
comments: 'Could do with a bit more cowbell.',
isSatisfied: false,
url: 'https://example.com/my-test-page/',
})
);
});
Expand Down

0 comments on commit a19c89a

Please sign in to comment.