Skip to content

Commit

Permalink
Complete tests for lib/record.js (#185)
Browse files Browse the repository at this point in the history
* Add tests for error states in record.js

* Add set and save test for record

* Cleanup delete tests

- Remove unneeded done
- Fix spelling issue

* Remove mocking of run action

And replace with the same mocking we use in all other files
  • Loading branch information
rmeritz authored Jun 17, 2020
1 parent 0e1018c commit 939fba5
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 74 deletions.
1 change: 0 additions & 1 deletion lib/record.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// istanbul ignore file
'use strict';

var assign = require('lodash/assign');
Expand Down
27 changes: 24 additions & 3 deletions test/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ describe('record deletion', function() {
});
});

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

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

it('can delete multiple records', function() {
return airtable
.base('app123')
Expand All @@ -52,7 +74,7 @@ describe('record deletion', function() {
});
});

it('can throw an error if delete fails', function(done) {
it('can throw an error if a multi-record delete fails', function() {
testExpressApp.set('handler override', function(req, res) {
res.status(402).json({
error: {message: 'foo bar'},
Expand All @@ -65,12 +87,11 @@ describe('record deletion', function() {
.destroy(['rec123', 'rec456'])
.then(
function() {
throw new Error('Promise unexpectly fufilled.');
throw new Error('Promise unexpectedly fufilled.');
},
function(err) {
expect(err.statusCode).toBe(402);
expect(err.message).toBe('foo bar');
done();
}
);
});
Expand Down
23 changes: 23 additions & 0 deletions test/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,27 @@ describe('record retrival', function() {
expect(foundRecord.get('Name')).toBe('Rebecca');
});
});

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

return airtable
.base('app123')
.table('Table')
.find('recabcd')
.then(
function() {
throw new Error('Promise unexpectly fufilled.');
},
function(err) {
expect(err.statusCode).toBe(402);
expect(err.message).toBe('foo bar');
done();
}
);
});
});
136 changes: 67 additions & 69 deletions test/record.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
'use strict';

var Record = require('../lib/record');
var testHelpers = require('./test_helpers');

describe('Record', function() {
var airtable;
var table;
var teardownAsync;
var testExpressApp;
var baseId = 'app123';

beforeEach(function() {
table = {
_base: {
runAction: jest.fn(),
},
_urlEncodedNameOrId: function() {
return 'My%20Table';
},
};
return testHelpers.getMockEnvironmentAsync().then(function(env) {
airtable = env.airtable;
teardownAsync = env.teardownAsync;
testExpressApp = env.testExpressApp;
table = airtable.base(baseId).table('Table');
});
});

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

it('can be initialized with a record ID and no data', function() {
Expand Down Expand Up @@ -65,6 +72,26 @@ describe('Record', function() {
});
});

describe('set', function() {
var record;
beforeEach(function() {
record = new Record(table, null, {
id: 'rec123',
fields: {foo: 'bar'},
});
});

it('sets a new value', function() {
record.set('bing', 'sing');
expect(record.get('bing')).toBe('sing');
});

it('re-sets an existing value', function() {
record.set('foo', 'pig');
expect(record.get('foo')).toBe('pig');
});
});

describe('patchUpdate', function() {
var record;

Expand All @@ -74,17 +101,14 @@ describe('Record', function() {
fields: {foo: 'bar'},
});

table._base.runAction.mockImplementationOnce(function(
method,
path,
queryParams,
bodyData,
callback
) {
callback(null, null, {
id: bodyData.id,
testExpressApp.set('handler override', function(req, res) {
expect(req.method).toBe('PATCH');
expect(req.url).toBe('/v0/app123/Table/rec123?');
expect(req.body).toStrictEqual({fields: {baz: 'qux'}});
res.json({
id: req.params.recordId,
fields: {foo: 'bar', baz: 'qux'},
createdTime: '2020-04-20T16:20:00.000Z',
fields: Object.assign({foo: 'bar'}, bodyData.fields),
});
});
});
Expand All @@ -96,17 +120,6 @@ describe('Record', function() {
expect(updatedRecord).toBe(record);
expect(record.get('foo')).toEqual('bar');
expect(record.get('baz')).toEqual('qux');

expect(table._base.runAction).toHaveBeenCalledWith(
'patch',
'/My%20Table/rec123',
{},
{
fields: {baz: 'qux'},
},
expect.any(Function)
);

done();
});
});
Expand All @@ -133,17 +146,14 @@ describe('Record', function() {
fields: {foo: 'bar'},
});

table._base.runAction.mockImplementationOnce(function(
method,
path,
queryParams,
bodyData,
callback
) {
callback(null, null, {
id: bodyData.id,
testExpressApp.set('handler override', function(req, res) {
expect(req.method).toBe('PUT');
expect(req.url).toBe('/v0/app123/Table/rec123?');
expect(req.body).toStrictEqual({fields: {baz: 'qux'}});
res.json({
id: req.params.recordId,
fields: {baz: 'qux'},
createdTime: '2020-04-20T16:20:00.000Z',
fields: Object.assign(bodyData.fields),
});
});
});
Expand All @@ -155,23 +165,25 @@ describe('Record', function() {
expect(updatedRecord).toBe(record);
expect(record.get('foo')).toBeUndefined();
expect(record.get('baz')).toEqual('qux');
done();
});
});

expect(table._base.runAction).toHaveBeenCalledWith(
'put',
'/My%20Table/rec123',
{},
{
fields: {baz: 'qux'},
},
expect.any(Function)
);
it('saves the record and calls a callback', function(done) {
record.set('foo', undefined); // eslint-disable-line no-undefined
record.set('baz', 'qux');
record.save(function(err, updatedRecord) {
expect(err).toBeNull();

expect(updatedRecord).toBe(record);
expect(record.get('foo')).toBeUndefined();
expect(record.get('baz')).toEqual('qux');
done();
});
});

it('returns a promise when no callback is passed', function() {
return record.patchUpdate({baz: 'qux'}).then(function(updatedRecord) {
return record.putUpdate({baz: 'qux'}).then(function(updatedRecord) {
expect(updatedRecord).toBe(record);
expect(record.get('foo')).toBeUndefined();
expect(record.get('baz')).toEqual('qux');
Expand All @@ -185,17 +197,13 @@ describe('Record', function() {

describe('fetch', function() {
beforeEach(function() {
table._base.runAction.mockImplementationOnce(function(
method,
path,
queryParams,
bodyData,
callback
) {
callback(null, null, {
id: 'rec123',
createdTime: '2020-04-20T16:20:00.000Z',
testExpressApp.set('handler override', function(req, res) {
expect(req.method).toBe('GET');
expect(req.url).toBe('/v0/app123/Table/rec123?');
res.json({
id: req.params.recordId,
fields: {foo: 'bar'},
createdTime: '2020-04-20T16:20:00.000Z',
});
});
});
Expand All @@ -205,19 +213,9 @@ describe('Record', function() {

record.fetch(function(err, fetchedRecord) {
expect(err).toBeNull();

expect(fetchedRecord).toBe(record);
expect(record.get('foo')).toBe('bar');
expect(record.get('baz')).toBeUndefined();

expect(table._base.runAction).toHaveBeenCalledWith(
'get',
'/My%20Table/rec123',
{},
null,
expect.any(Function)
);

done();
});
});
Expand Down
54 changes: 53 additions & 1 deletion test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ describe('record updates', function() {
});
});

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

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

it('can update two records', function() {
return airtable
.base('app123')
Expand Down Expand Up @@ -166,7 +192,7 @@ describe('record updates', function() {
});
});

it('can throw an error if update fails', function(done) {
it('can throw an error if a multi-record update fails', function(done) {
testExpressApp.set('handler override', function(req, res) {
res.status(402).json({
error: {message: 'foo bar'},
Expand Down Expand Up @@ -233,6 +259,32 @@ describe('record updates', function() {
});
});

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

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

it('can update one record with an array', function() {
return airtable
.base('app123')
Expand Down

0 comments on commit 939fba5

Please sign in to comment.