Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete tests for lib/record.js #185

Merged
merged 4 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 24 additions & 1 deletion test/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ describe('record deletion', function() {
});
});

it('can throw an error if a single record 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')
.then(
function() {
throw new Error('Promise unexpectly fufilled.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "unexpectly" -> "unexpectedly" (here and below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

},
function(err) {
expect(err.statusCode).toBe(402);
expect(err.message).toBe('foo bar');
done();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The done callback can be omitted when tests return a Promise (here and below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Digging the pun

}
);
});

it('can delete multiple records', function() {
return airtable
.base('app123')
Expand All @@ -52,7 +75,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(done) {
rmeritz marked this conversation as resolved.
Show resolved Hide resolved
testExpressApp.set('handler override', function(req, res) {
res.status(402).json({
error: {message: 'foo bar'},
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();
}
);
});
});
44 changes: 44 additions & 0 deletions test/record.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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 Down Expand Up @@ -170,6 +190,30 @@ describe('Record', 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');

expect(table._base.runAction).toHaveBeenCalledWith(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The invocation of _base.runAction doesn't seem particularly important for the caller. A more robust test would involve verifying that an HTTP PUT request is issued. This file sets the precedent, so good on you for finding that and sticking to it! Improving the existing tests is in-scope for our work, but I'll be satisfied if you'd rather address this in a follow-up patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated these tests to use the same mocking we use in all other places.

'put',
'/My%20Table/rec123',
{},
{
fields: {baz: 'qux'},
},
expect.any(Function)
);

done();
});
});

it('returns a promise when no callback is passed', function() {
return record.patchUpdate({baz: 'qux'}).then(function(updatedRecord) {
expect(updatedRecord).toBe(record);
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