From 60df3a98535d61910d9e4141f2a9ea52165731b8 Mon Sep 17 00:00:00 2001 From: Yoann Maitre Date: Tue, 4 Jun 2019 19:20:49 +0200 Subject: [PATCH] Merge all validation tests in a single file --- test/integration/validations-case.test.ts | 86 --------- test/integration/validations.test.ts | 217 ++++++++++++++-------- 2 files changed, 139 insertions(+), 164 deletions(-) delete mode 100644 test/integration/validations-case.test.ts diff --git a/test/integration/validations-case.test.ts b/test/integration/validations-case.test.ts deleted file mode 100644 index 47fb094..0000000 --- a/test/integration/validations-case.test.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { expect, sinon, fetchMock } from "../test-helper" -import { Person, PersonDetail } from "../fixtures" -import { tempId } from "../../src/util/temp-id" -import { SpraypaintBase, ModelRecord } from "../../src/model" -import { ValidationError } from "../../src/validation-errors" - -const mockErrors = { - personDetailBase: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "base some error", - meta: { - relationship: { - name: "person_detail", - type: "person_details", - ["temp-id"]: "abc1", - id: "1", - attribute: "base", - message: "some error" - } - } - } -} as any - -const resetMocks = () => { - fetchMock.restore() - - let errors = [] - - for (let key in mockErrors) { - errors.push(mockErrors[key]) - } - - fetchMock.mock({ - matcher: "*", - response: { - status: 422, - body: { - errors - } - } - }) -} - -let instance: Person -let tempIdIndex = 0 -describe("validations", () => { - beforeEach(() => { - resetMocks() - }) - - beforeEach(() => { - sinon.stub(tempId, "generate").callsFake(() => { - tempIdIndex++ - return `abc${tempIdIndex}` - }) - - const personDetail = new PersonDetail({ id: "1" }) - personDetail.isPersisted = true - instance = new Person({ - personDetail - }) - }) - - afterEach(() => { - tempIdIndex = 0 - ;(tempId.generate).restore() - }) - - it("applies errors to nested belongsTo relationships with underscores", async () => { - const isSuccess = await instance.save({ with: ["person_details"] }) - expect(instance.isPersisted).to.eq(false) - expect(isSuccess).to.eq(false) - expect(instance.personDetail.errors).to.deep.equal({ - base: { - title: "Validation Error", - attribute: "base", - code: "unprocessable_entity", - fullMessage: "some error", - message: "some error", - rawPayload: mockErrors.personDetailBase - } - }) - }) -}) diff --git a/test/integration/validations.test.ts b/test/integration/validations.test.ts index 39beb57..41e7f36 100644 --- a/test/integration/validations.test.ts +++ b/test/integration/validations.test.ts @@ -1,82 +1,10 @@ import { expect, sinon, fetchMock } from "../test-helper" -import { Author, Book, Genre } from "../fixtures" +import { Author, Book, Genre, Person, PersonDetail } from "../fixtures" import { tempId } from "../../src/util/temp-id" import { SpraypaintBase, ModelRecord } from "../../src/model" import { ValidationError } from "../../src/validation-errors" -const mockErrors = { - firstName: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "First Name cannot be blank", - meta: { attribute: "first_name", message: "cannot be blank" } - }, - lastName: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "Last Name cannot be blank", - meta: { attribute: "last-name", message: "cannot be blank" } - }, - bookTitle: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "Title cannot be blank", - meta: { - relationship: { - name: "books", - type: "books", - ["temp-id"]: "abc1", - attribute: "title", - message: "cannot be blank" - } - } - }, - bookGenreName: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "Name cannot be blank", - meta: { - relationship: { - name: "books", - type: "books", - ["temp-id"]: "abc1", - relationship: { - name: "genre", - type: "genres", - id: "1", - attribute: "name", - message: "cannot be blank" - } - } - } - }, - bookGenreBase: { - code: "unprocessable_entity", - status: "422", - title: "Validation Error", - detail: "base some error", - meta: { - relationship: { - name: "books", - type: "books", - ["temp-id"]: "abc1", - relationship: { - name: "genre", - type: "genres", - id: "1", - attribute: "base", - message: "some error" - } - } - } - } -} as any - -const resetMocks = () => { +const resetMocks = (mockErrors: any) => { fetchMock.restore() let errors = [] @@ -96,11 +24,84 @@ const resetMocks = () => { }) } -let instance: Author let tempIdIndex = 0 -describe("validations", () => { +describe("validations (1/2)", () => { + const mockErrors = { + firstName: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "First Name cannot be blank", + meta: { attribute: "first_name", message: "cannot be blank" } + }, + lastName: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "Last Name cannot be blank", + meta: { attribute: "last-name", message: "cannot be blank" } + }, + bookTitle: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "Title cannot be blank", + meta: { + relationship: { + name: "books", + type: "books", + ["temp-id"]: "abc1", + attribute: "title", + message: "cannot be blank" + } + } + }, + bookGenreName: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "Name cannot be blank", + meta: { + relationship: { + name: "books", + type: "books", + ["temp-id"]: "abc1", + relationship: { + name: "genre", + type: "genres", + id: "1", + attribute: "name", + message: "cannot be blank" + } + } + } + }, + bookGenreBase: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "base some error", + meta: { + relationship: { + name: "books", + type: "books", + ["temp-id"]: "abc1", + relationship: { + name: "genre", + type: "genres", + id: "1", + attribute: "base", + message: "some error" + } + } + } + } + } as any + + let instance: Author + beforeEach(() => { - resetMocks() + resetMocks(mockErrors) }) beforeEach(() => { @@ -108,7 +109,6 @@ describe("validations", () => { tempIdIndex++ return `abc${tempIdIndex}` }) - instance = new Author({ lastName: "King" }) const genre = new Genre({ id: "1" }) genre.isPersisted = true @@ -279,3 +279,64 @@ describe("validations", () => { }) }) }) + +describe("validations (2/2)", () => { + const mockErrors = { + personDetailBase: { + code: "unprocessable_entity", + status: "422", + title: "Validation Error", + detail: "base some error", + meta: { + relationship: { + name: "person_detail", + type: "person_details", + ["temp-id"]: "abc1", + id: "1", + attribute: "base", + message: "some error" + } + } + } + } as any + + let instance: Person + + beforeEach(() => { + resetMocks(mockErrors) + }) + + beforeEach(() => { + sinon.stub(tempId, "generate").callsFake(() => { + tempIdIndex++ + return `abc${tempIdIndex}` + }) + + const personDetail = new PersonDetail({ id: "1" }) + personDetail.isPersisted = true + instance = new Person({ + personDetail + }) + }) + + afterEach(() => { + tempIdIndex = 0 + ;(tempId.generate).restore() + }) + + it("applies errors to nested belongsTo relationships with underscores", async () => { + const isSuccess = await instance.save({ with: ["person_details"] }) + expect(instance.isPersisted).to.eq(false) + expect(isSuccess).to.eq(false) + expect(instance.personDetail.errors).to.deep.equal({ + base: { + title: "Validation Error", + attribute: "base", + code: "unprocessable_entity", + fullMessage: "some error", + message: "some error", + rawPayload: mockErrors.personDetailBase + } + }) + }) +})