Skip to content

Commit

Permalink
Add test coverage for lib/table.js (#178)
Browse files Browse the repository at this point in the history
* Add tests initial tests for table.js

* Add Table#select tests

* Add table tests for handling of error cases

- Is there a better way to rest the 'handler override' than switching to
beforeEach/afterEach?

* Add tests to Table#list

* Add tests for Table#forEach

* Remove unused fetchNextPage from select tests

* Improve error tests by adding status and msg check

* Improve forEach tests

- Add tests with more than one record
- Add test with no records
- Add iteration counter to ensure all expectations are run

* Ensure testing correct errors in select test

* Use then with an error statement instead of catch

To test that errors are thrown in tests. This way if tests unexpectly
fufill the promise they immediately error instead of just timeing out.
  • Loading branch information
rmeritz authored Jun 10, 2020
1 parent b91a452 commit cb41a4f
Show file tree
Hide file tree
Showing 8 changed files with 577 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// istanbul ignore file
'use strict';

var isArray = require('lodash/isArray');
Expand Down
35 changes: 33 additions & 2 deletions test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ var testHelpers = require('./test_helpers');

describe('record creation', function() {
var airtable;
var testExpressApp;
var teardownAsync;

beforeAll(function() {
beforeEach(function() {
return testHelpers.getMockEnvironmentAsync().then(function(env) {
airtable = env.airtable;
testExpressApp = env.testExpressApp;
teardownAsync = env.teardownAsync;
});
});

afterAll(function() {
afterEach(function() {
return teardownAsync();
});

Expand Down Expand Up @@ -51,6 +53,35 @@ describe('record creation', function() {
);
});

it('can throw an error if create fails', function(done) {
testExpressApp.set('handler override', function(req, res) {
res.status(402).json({
error: {message: 'foo bar'},
});
});

return airtable
.base('app123')
.table('Table')
.create(
{
foo: 'boo',
bar: 'yar',
},
{typecast: true}
)
.then(
function() {
throw new Error('Promise unexpectly fufilled.');
},
function(err) {
expect(err.statusCode).toBe(402);
expect(err.message).toBe('foo bar');
done();
}
);
});

it('can add the "typecast" parameter when creating one record', function() {
return airtable
.base('app123')
Expand Down
29 changes: 27 additions & 2 deletions test/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ var testHelpers = require('./test_helpers');

describe('record deletion', function() {
var airtable;
var testExpressApp;
var teardownAsync;

beforeAll(function() {
beforeEach(function() {
return testHelpers.getMockEnvironmentAsync().then(function(env) {
airtable = env.airtable;
testExpressApp = env.testExpressApp;
teardownAsync = env.teardownAsync;
});
});

afterAll(function() {
afterEach(function() {
return teardownAsync();
});

Expand Down Expand Up @@ -50,6 +52,29 @@ describe('record deletion', function() {
});
});

it('can throw an error if delete fails', function(done) {
testExpressApp.set('handler override', function(req, res) {
res.status(402).json({
error: {message: 'foo bar'},
});
});

return airtable
.base('app123')
.table('Table')
.destroy(['rec123', 'rec456'])
.then(
function() {
throw new Error('Promise unexpectly fufilled.');
},
function(err) {
expect(err.statusCode).toBe(402);
expect(err.message).toBe('foo bar');
done();
}
);
});

it('can delete multiple records and call a callback', function(done) {
airtable
.base('app123')
Expand Down
44 changes: 44 additions & 0 deletions test/find.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var testHelpers = require('./test_helpers');

describe('record retrival', function() {
var airtable;
var testExpressApp;
var teardownAsync;

beforeAll(function() {
return testHelpers.getMockEnvironmentAsync().then(function(env) {
airtable = env.airtable;
testExpressApp = env.testExpressApp;
teardownAsync = env.teardownAsync;
});
});

afterAll(function() {
return teardownAsync();
});

it('can find one record', function() {
var recordId = 'record1';

testExpressApp.set('handler override', function(req, res) {
expect(req.method).toBe('GET');
expect(req.url).toBe('/v0/app123/Table/record1?');
res.json({
id: req.params.recordId,
fields: {Name: 'Rebecca'},
createdTime: '2020-04-20T16:20:00.000Z',
});
});

return airtable
.base('app123')
.table('Table')
.find(recordId)
.then(function(foundRecord) {
expect(foundRecord.id).toBe(recordId);
expect(foundRecord.get('Name')).toBe('Rebecca');
});
});
});
Loading

0 comments on commit cb41a4f

Please sign in to comment.