|
| 1 | +import { expect, sinon, fetchMock } from '../test-helper'; |
| 2 | +import { Author, Book, Genre } from '../fixtures'; |
| 3 | +import uuid from '../../src/util/uuid'; |
| 4 | + |
| 5 | +let serverResponse; |
| 6 | + |
| 7 | +const resetMocks = function() { |
| 8 | + fetchMock.restore(); |
| 9 | + |
| 10 | + fetchMock.mock({ |
| 11 | + matcher: '*', |
| 12 | + response: { |
| 13 | + status: 422, |
| 14 | + body: { |
| 15 | + errors: [ |
| 16 | + { |
| 17 | + code: 'unprocessable_entity', |
| 18 | + status: '422', |
| 19 | + title: 'Validation Error', |
| 20 | + detail: 'First Name cannot be blank', |
| 21 | + meta: { attribute: 'first_name', message: 'cannot be blank' } |
| 22 | + }, |
| 23 | + { |
| 24 | + code: 'unprocessable_entity', |
| 25 | + status: '422', |
| 26 | + title: 'Validation Error', |
| 27 | + detail: 'Last Name cannot be blank', |
| 28 | + meta: { attribute: 'last_name', message: 'cannot be blank' } |
| 29 | + }, |
| 30 | + { |
| 31 | + code: 'unprocessable_entity', |
| 32 | + status: '422', |
| 33 | + title: 'Validation Error', |
| 34 | + detail: 'Title cannot be blank', |
| 35 | + meta: { |
| 36 | + relationship: { |
| 37 | + name: 'books', |
| 38 | + type: 'books', |
| 39 | + ['temp-id']: 'abc1', |
| 40 | + attribute: 'title', |
| 41 | + message: 'cannot be blank' |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + code: 'unprocessable_entity', |
| 47 | + status: '422', |
| 48 | + title: 'Validation Error', |
| 49 | + detail: 'Name cannot be blank', |
| 50 | + meta: { |
| 51 | + relationship: { |
| 52 | + name: 'books', |
| 53 | + type: 'books', |
| 54 | + ['temp-id']: 'abc1', |
| 55 | + relationship: { |
| 56 | + name: 'genre', |
| 57 | + type: 'genres', |
| 58 | + id: '1', |
| 59 | + attribute: 'name', |
| 60 | + message: 'cannot be blank' |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + fetchMock.post('http://example.com/api/v1/people', function(url, payload) { |
| 71 | + return serverResponse; |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +let instance; |
| 76 | +let tempIdIndex = 0; |
| 77 | +describe('validations', function() { |
| 78 | + beforeEach(function () { |
| 79 | + resetMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + beforeEach(function() { |
| 83 | + sinon.stub(uuid, 'generate').callsFake(function() { |
| 84 | + tempIdIndex++ |
| 85 | + return `abc${tempIdIndex}`; |
| 86 | + }); |
| 87 | + |
| 88 | + instance = new Author({ lastName: 'King' }); |
| 89 | + let genre = new Genre({ id: '1' }); |
| 90 | + genre.isPersisted(true); |
| 91 | + let book = new Book({ title: 'blah', genre }); |
| 92 | + instance.books = [book] |
| 93 | + }); |
| 94 | + |
| 95 | + afterEach(function() { |
| 96 | + tempIdIndex = 0; |
| 97 | + uuid.generate['restore'](); |
| 98 | + }); |
| 99 | + |
| 100 | + // todo on next save, remove errs |
| 101 | + it('applies errors to the instance', function(done) { |
| 102 | + instance.save({ with: { books: 'genre' }}).then((success) => { |
| 103 | + expect(instance.isPersisted()).to.eq(false); |
| 104 | + expect(success).to.eq(false); |
| 105 | + expect(instance.errors).to.deep.equal({ |
| 106 | + first_name: 'cannot be blank', |
| 107 | + last_name: 'cannot be blank' |
| 108 | + }); |
| 109 | + done(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('applies errors to nested hasMany relationships', function(done) { |
| 114 | + instance.save({ with: { books: 'genre' }}).then((success) => { |
| 115 | + expect(instance.isPersisted()).to.eq(false); |
| 116 | + expect(success).to.eq(false); |
| 117 | + expect(instance.books[0].errors).to.deep.equal({ |
| 118 | + title: 'cannot be blank', |
| 119 | + }); |
| 120 | + done(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('applies errors to nested belongsTo relationships', function(done) { |
| 125 | + instance.save({ with: { books: 'genre' }}).then((success) => { |
| 126 | + expect(instance.isPersisted()).to.eq(false); |
| 127 | + expect(success).to.eq(false); |
| 128 | + console.log(instance.books[0].genre.errors) |
| 129 | + expect(instance.books[0].genre.errors).to.deep.equal({ |
| 130 | + name: 'cannot be blank', |
| 131 | + }); |
| 132 | + done(); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments