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

Add partial tests for Record class #104

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function(grunt) {
setTimeout: true,
jest: true,
describe: true,
beforeEach: true,
beforeAll: true,
afterAll: true,
it: true,
Expand Down
201 changes: 201 additions & 0 deletions test/record.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
'use strict';

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

describe('Record', function () {
var table;

beforeEach(function () {
table = {
_base: {
runAction: jest.fn(),
},
_urlEncodedNameOrId: function () {
return 'My%20Table';
},
};
});

it('can be initialized with a record ID and no data', function () {
var record = new Record(table, 'rec123');
expect(record.id).toBe('rec123');
expect(record.fields).toEqual({});
});

it('can be initialized with data, which contains the ID', function () {
var record = new Record(table, null, {
id: 'rec123',
fields: {foo: 'bar'},
});
expect(record.id).toBe('rec123');
expect(record.fields).toEqual({foo: 'bar'});
expect(record.get('foo')).toBe('bar');
});

it('can be initialized with data and an ID (the explicit ID wins)', function () {
var record = new Record(table, 'rec123', {
id: 'recIGNORED',
fields: {foo: 'bar'},
});
expect(record.id).toBe('rec123');
expect(record.fields).toEqual({foo: 'bar'});
});

describe('getId', function () {
it('returns the record ID', function () {
expect(new Record(table, 'rec123').getId()).toBe('rec123');
});
});

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

it("returns a field's value if set", function () {
expect(record.get('foo')).toBe('bar');
});

it("returns undefined if requesting a cell value that doesn't exist", function () {
expect(record.get('missing')).toBeUndefined();
});
});

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

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

table._base.runAction.mockImplementationOnce(function (method, path, queryParams, bodyData, callback) {
callback(null, null, {
id: bodyData.id,
createdTime: '2020-04-20T16:20:00.000Z',
fields: Object.assign({foo: 'bar'}, bodyData.fields),
});
});
});

it('patch-updates the record and calls a callback', function (done) {
record.patchUpdate({baz: 'qux'}, function (err, updatedRecord) {
expect(err).toBeNull();

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();
});
});

it('returns a promise when no callback is passed', function () {
return record.patchUpdate({baz: 'qux'})
.then(function (updatedRecord) {
expect(updatedRecord).toBe(record);
expect(record.get('foo')).toEqual('bar');
expect(record.get('baz')).toEqual('qux');
});
});

it('aliases "updateFields"', function () {
expect(record.updateFields).toBe(record.patchUpdate);
});
});

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

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

table._base.runAction.mockImplementationOnce(function (method, path, queryParams, bodyData, callback) {
callback(null, null, {
id: bodyData.id,
createdTime: '2020-04-20T16:20:00.000Z',
fields: Object.assign(bodyData.fields),
});
});
});

it('patch-updates the record and calls a callback', function (done) {
record.putUpdate({baz: 'qux'}, 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('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);
expect(record.get('foo')).toBeUndefined();
expect(record.get('baz')).toEqual('qux');
});
});

it('aliases "replaceFields"', function () {
expect(record.replaceFields).toBe(record.putUpdate);
});
});

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',
fields: {foo: 'bar'},
});
});
});

it('fetches a record and calls a callback', function (done) {
var record = new Record(table, 'rec123');

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();
});
});

it('returns a promise when no callback is passed', function () {
var record = new Record(table, 'rec123');

return record.fetch().then(function (fetchedRecord) {
expect(fetchedRecord).toBe(record);
expect(record.get('foo')).toBe('bar');
expect(record.get('baz')).toBeUndefined();
});
});
});
});