Skip to content

Commit

Permalink
Refactored tests to use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
Clement Allen committed Oct 2, 2018
1 parent c848d5c commit 2e24d9b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 116 deletions.
104 changes: 48 additions & 56 deletions test/src/Caller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,40 @@ describe('Caller', () => {
return expect(caller.get(mockPath)).to.be.rejectedWith('Caller Unavailable');
});

it('should call axios with the instance config if no separate config provided', (done) => {
it('should call axios with the instance config if no separate config provided', async () => {
axiosStub.resolves(mockResponse);
caller.get(mockPath).then(() => {
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'GET',
url: 'http://example.com/test/path',
timeout: 5000,
auth: false
});
done();
await caller.get(mockPath);
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'GET',
url: 'http://example.com/test/path',
timeout: 5000,
auth: false
});
});

it('should call axios with the individual config if provided', (done) => {
it('should call axios with the individual config if provided', async () => {
const newMockConfig = Object.assign({}, mockConfig, {
params: {
tree: 'tree'
}
});
axiosStub.resolves(mockResponse);
caller.get(mockPath, newMockConfig).then(() => {
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'GET',
url: 'http://example.com/test/path',
timeout: 1000,
params: {
tree: 'tree'
},
auth: {
username: 'user',
password: 'pass'
}
});
done();
await caller.get(mockPath, newMockConfig);
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'GET',
url: 'http://example.com/test/path',
timeout: 1000,
params: {
tree: 'tree'
},
auth: {
username: 'user',
password: 'pass'
}
});
});
});
Expand All @@ -113,44 +109,40 @@ describe('Caller', () => {
return expect(caller.post(mockPath)).to.be.rejectedWith('Caller Unavailable');
});

it('should call axios with the instance config if no separate config provided', (done) => {
it('should call axios with the instance config if no separate config provided', async () => {
axiosStub.resolves(mockResponse);
caller.post(mockPath).then(() => {
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'POST',
url: 'http://example.com/test/path',
timeout: 5000,
auth: false
});
done();
await caller.post(mockPath);
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'POST',
url: 'http://example.com/test/path',
timeout: 5000,
auth: false
});
});

it('should call axios with the individual config if provided', (done) => {
it('should call axios with the individual config if provided', async () => {
const newMockConfig = Object.assign({}, mockConfig, {
params: {
tree: 'tree'
}
});
axiosStub.resolves(mockResponse);
caller.post(mockPath, newMockConfig).then(() => {
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'POST',
url: 'http://example.com/test/path',
timeout: 1000,
params: {
tree: 'tree'
},
auth: {
username: 'user',
password: 'pass'
}
});
done();
await caller.post(mockPath, newMockConfig);
expect(axiosStub).to.be.calledWithExactly({
baseURL: mockUrl,
headers: {},
method: 'POST',
url: 'http://example.com/test/path',
timeout: 1000,
params: {
tree: 'tree'
},
auth: {
username: 'user',
password: 'pass'
}
});
});
});
Expand Down
96 changes: 36 additions & 60 deletions test/src/Smithers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ describe('Smithers', () => {
return expect(smithers.info()).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.info(mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/api/json', mockConfig);
done();
});
await smithers.info(mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/api/json', mockConfig);
});
});

Expand All @@ -98,12 +96,10 @@ describe('Smithers', () => {
return expect(smithers.job('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.job('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/api/json', mockConfig);
done();
});
await smithers.job('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.job()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -120,12 +116,10 @@ describe('Smithers', () => {
return expect(smithers.build('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerPostStub.resolves(mockResponse);
smithers.build('jobName', mockConfig).then(() => {
expect(callerPostStub).to.be.calledWithExactly('/job/jobName/build', mockConfig);
done();
});
await smithers.build('jobName', mockConfig);
expect(callerPostStub).to.be.calledWithExactly('/job/jobName/build', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.build()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -142,12 +136,10 @@ describe('Smithers', () => {
return expect(smithers.lastBuild('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.lastBuild('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastBuild/api/json', mockConfig);
done();
});
await smithers.lastBuild('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastBuild/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.lastBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -164,12 +156,10 @@ describe('Smithers', () => {
return expect(smithers.lastSuccessfulBuild('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.lastSuccessfulBuild('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastSuccessfulBuild/api/json', mockConfig);
done();
});
await smithers.lastSuccessfulBuild('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastSuccessfulBuild/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.lastSuccessfulBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -186,12 +176,10 @@ describe('Smithers', () => {
return expect(smithers.lastStableBuild('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.lastStableBuild('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastStableBuild/api/json', mockConfig);
done();
});
await smithers.lastStableBuild('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastStableBuild/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.lastStableBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -208,12 +196,10 @@ describe('Smithers', () => {
return expect(smithers.lastUnsuccessfulBuild('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.lastUnsuccessfulBuild('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastUnsuccessfulBuild/api/json', mockConfig);
done();
});
await smithers.lastUnsuccessfulBuild('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastUnsuccessfulBuild/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.lastUnsuccessfulBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -230,12 +216,10 @@ describe('Smithers', () => {
return expect(smithers.lastFailedBuild('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.lastFailedBuild('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastFailedBuild/api/json', mockConfig);
done();
});
await smithers.lastFailedBuild('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/lastFailedBuild/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.lastFailedBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -254,12 +238,10 @@ describe('Smithers', () => {
return expect(smithers.specificBuild('jobName', buildNumber)).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.specificBuild('jobName', buildNumber, mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/100/api/json', mockConfig);
done();
});
await smithers.specificBuild('jobName', buildNumber, mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/100/api/json', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.specificBuild()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -278,12 +260,10 @@ describe('Smithers', () => {
return expect(smithers.configXML('jobName')).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.configXML('jobName', mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/config.xml', mockConfig);
done();
});
await smithers.configXML('jobName', mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/job/jobName/config.xml', mockConfig);
});

it('should throw an error if the name parameter is not provided', () => expect(smithers.configXML()).to.be.rejectedWith('Missing parameter: name'));
Expand All @@ -300,12 +280,10 @@ describe('Smithers', () => {
return expect(smithers.overallLoad()).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.overallLoad(mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/overallLoad/api/json', mockConfig);
done();
});
await smithers.overallLoad(mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/overallLoad/api/json', mockConfig);
});
});

Expand All @@ -320,12 +298,10 @@ describe('Smithers', () => {
return expect(smithers.queue()).to.be.rejectedWith(mockError.message);
});

it('should call Caller.get with the expected parameters', (done) => {
it('should call Caller.get with the expected parameters', async () => {
callerGetStub.resolves(mockResponse);
smithers.queue(mockConfig).then(() => {
expect(callerGetStub).to.be.calledWithExactly('/queue/api/json', mockConfig);
done();
});
await smithers.queue(mockConfig);
expect(callerGetStub).to.be.calledWithExactly('/queue/api/json', mockConfig);
});
});
});

0 comments on commit 2e24d9b

Please sign in to comment.