Skip to content

Commit

Permalink
Merge all validation tests in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
yoann-hellopret committed Jun 4, 2019
1 parent 97638e0 commit 60df3a9
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 164 deletions.
86 changes: 0 additions & 86 deletions test/integration/validations-case.test.ts

This file was deleted.

217 changes: 139 additions & 78 deletions test/integration/validations.test.ts
Original file line number Diff line number Diff line change
@@ -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 = []
Expand All @@ -96,19 +24,91 @@ 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(() => {
sinon.stub(tempId, "generate").callsFake(() => {
tempIdIndex++
return `abc${tempIdIndex}`
})

instance = new Author({ lastName: "King" })
const genre = new Genre({ id: "1" })
genre.isPersisted = true
Expand Down Expand Up @@ -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
;(<any>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
}
})
})
})

0 comments on commit 60df3a9

Please sign in to comment.