Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/derived-value.subtest.js
const assert = require("assert"); | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const assert = require("assert"); | ||||
const { with_running_app, with_stopped_app } = locreq( | const { with_running_app } = locreq("test_utils/with-test-app.js"); | ||||
"test_utils/with-test-app.js" | |||||
); | |||||
const assert_throws_async = locreq("test_utils/assert_throws_async.js"); | const assert_throws_async = locreq("test_utils/assert_throws_async.js"); | ||||
const make_test_collection = (app, derived_value_params) => () => | const make_test_collection = (app, derived_value_params) => () => | ||||
app.createChip(app.Sealious.Collection, { | app.createChip(app.Sealious.Collection, { | ||||
name: "people", | name: "people", | ||||
fields: [ | fields: [ | ||||
{ | { | ||||
name: "username", | name: "username", | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | it("throws an error if given value that is not proper for some field", async () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
await make_test_collection(app, { | await make_test_collection(app, { | ||||
fields: ["username", "surname"], | fields: ["username", "surname"], | ||||
derived_fn: async (username, surname) => | derived_fn: async (username, surname) => | ||||
`${username} ${surname}`, | `${username} ${surname}`, | ||||
})(); | })(); | ||||
await assert_throws_async( | await assert_throws_async( | ||||
async () => { | async () => | ||||
const person = await rest_api.post( | rest_api.post("/api/v1/collections/people", { | ||||
"/api/v1/collections/people", | |||||
{ | |||||
username: "Antoine", | username: "Antoine", | ||||
surname: 123123, | surname: 123123, | ||||
} | }), | ||||
); | |||||
}, | |||||
error => | error => | ||||
assert.deepEqual( | assert.deepEqual( | ||||
error.response.data.data.surname.message, | error.response.data.data.surname.message, | ||||
"Type of 123123 is number, not string." | "Type of 123123 is number, not string." | ||||
) | ) | ||||
); | ); | ||||
})); | })); | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | describe("derived-value", () => { | ||||
it("throws when the value returned from derived_fn is unnacceptable by target_field_type of derived-value", async () => | it("throws when the value returned from derived_fn is unnacceptable by target_field_type of derived-value", async () => | ||||
with_running_app(async ({ app, rest_api }) => { | with_running_app(async ({ app, rest_api }) => { | ||||
await make_test_collection(app, { | await make_test_collection(app, { | ||||
fields: ["username", "surname"], | fields: ["username", "surname"], | ||||
derived_fn: async (username, surname) => 555, | derived_fn: async (username, surname) => 555, | ||||
})(); | })(); | ||||
await assert_throws_async( | await assert_throws_async( | ||||
async () => { | async () => | ||||
const person = await rest_api.post( | rest_api.post("/api/v1/collections/people", { | ||||
"/api/v1/collections/people", | |||||
{ | |||||
username: "Jan", | username: "Jan", | ||||
surname: "Kowalski", | surname: "Kowalski", | ||||
} | }), | ||||
); | |||||
}, | |||||
error => { | error => { | ||||
assert.deepEqual( | assert.deepEqual( | ||||
error.response.data.data.name_and_surname.message, | error.response.data.data.name_and_surname.message, | ||||
"Type of 555 is number, not string." | "Type of 555 is number, not string." | ||||
); | ); | ||||
} | } | ||||
); | ); | ||||
})); | })); | ||||
}); | }); |