From d7bafcfec405839d25e2ff0c26d7a0678efd94c8 Mon Sep 17 00:00:00 2001 From: "Dion Maicon E. Duarte" Date: Thu, 12 Dec 2019 20:10:03 -0200 Subject: [PATCH 1/3] Add types verification --- js/types.js | 154 +++++++++++++++++++++++++++++++++++++++++ test/types.test.js | 167 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 321 insertions(+) create mode 100644 js/types.js create mode 100644 test/types.test.js diff --git a/js/types.js b/js/types.js new file mode 100644 index 0000000..53b36f4 --- /dev/null +++ b/js/types.js @@ -0,0 +1,154 @@ +const ONE_TO_ONE = "oneToOne"; +const ONE_TO_MANY = "oneToMany"; +const TEXT = "text"; +const SELECT = "select"; +const TEXTAREA = "textarea"; +const NUMBER = "number"; +const CURRENCY = "currency"; +const HTML = "html"; +const RADIO = "radio"; +const CHECKBOX = "checkbox"; +const FILE = "file"; +const OBJECT = "object"; + +const HTML5_TYPES = [ + "color", + "date", + "datetime-local", + "email", + "hidden", + "image", + "month", + "password", + "range", + "reset", + "search", + "submit", + "tel", + "time", + "url", + "week" +]; + +const Types = class { + constructor() {} + + isValid() {} + + static get ONE_TO_ONE() { + return ONE_TO_ONE; + } + + static get ONE_TO_MANY() { + return ONE_TO_MANY; + } + + static get TEXT() { + return TEXT; + } + + static get SELECT() { + return SELECT; + } + + static get TEXTAREA() { + return TEXTAREA; + } + + static get NUMBER() { + return NUMBER; + } + + static get CURRENCY() { + return CURRENCY; + } + + static get HTML() { + return HTML; + } + + static get RADIO() { + return RADIO; + } + + static get CHECKBOX() { + return CHECKBOX; + } + + static get FILE() { + return FILE; + } + + static get OBJECT() { + return OBJECT; + } + + static get validTypes() { + return HTML5_TYPES.concat([ + ONE_TO_ONE, + ONE_TO_MANY, + TEXT, + SELECT, + TEXTAREA, + NUMBER, + CURRENCY, + HTML, + RADIO, + CHECKBOX, + FILE, + OBJECT + ]); + } + + static isValid(value) { + let types = this.validTypes; + return types.find(type => type == value); + } + + static modelIsValid(model) { + let response = { + message: "", + success: true + }; + + for (let prop in model) { + if (model.hasOwnProperty(prop)) { + let object = model[prop]; + switch (model[prop].type) { + case Types.ONE_TO_ONE: + case Types.ONE_TO_MANY: + if (!object.attribute) { + response.message += `Model type: ${prop} needs attribute property.\n`; + response.success = false; + } + if (!object.model) { + response.message += `Model type: ${prop} needs model property.\n`; + response.success = false; + } + break; + case Types.RADIO: + case Types.CHECKBOX: + case Types.SELECT: + if (!object.options) { + response.message += `Model type: ${prop} needs options property.\n`; + response.success = false; + } + break; + } + if (!this.isValid(model[prop].type)) { + response.message += `Model type: ${prop} is invalid.\n`; + response.success = false; + } + } + } + + if (!response.success) { + return response; + } else { + response.message = "Model is valid."; + return response; + } + } +}; + +module.exports = Types; diff --git a/test/types.test.js b/test/types.test.js new file mode 100644 index 0000000..7daab72 --- /dev/null +++ b/test/types.test.js @@ -0,0 +1,167 @@ +const Types = require("../js/types"); + +const model = { + title: { + type: Types.TEXT, + required: true + }, + ISBN: { + type: Types.NUMBER, + required: true + }, + authors: { + type: Types.ONE_TO_MANY, + attribute: "name", + model: "author" + }, + publishing: { + type: Types.ONE_TO_ONE, + attribute: "name", + model: "publishing" + }, + price: { + type: Types.CURRENCY + }, + birth: { + type: "date", + required: true + }, + active: { + type: Types.RADIO, + options: [ + { id: "Active", value: true }, + { id: "Inactive", value: false } + ] + }, + sponsor: { + type: Types.SELECT, + options: ["Patron", "Vue-Crudgen"] + } +}; + +test("Check if types are valid", () => { + let count = 0; + + for (let prop in model) { + if (model.hasOwnProperty(prop)) { + if (Types.isValid(model[prop].type)) { + count++; + } + } + } + expect(count).toBe(8); +}); + +test("Check if Types Models is invalid", () => { + let model = { + sponsor: { + type: "selects", + options: ["Patron", "Vue-Crudgen"] + } + }; + + let count; + + for (let prop in model) { + if (model.hasOwnProperty(prop)) { + if (Types.isValid(model[prop].type)) { + count++; + } + } + } + expect(count).not.toBe(1); +}); + +test("Check if model is valid", () => { + let response = Types.modelIsValid(model); + expect(response).toEqual({ + message: "Model is valid.", + success: true + }); +}); + +test("Check if model is invalid (ONE_TO_ONE)", () => { + model.supply = { + type: Types.ONE_TO_ONE + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch("Model type: supply needs model property."); + + expect(response.message).toMatch( + "Model type: supply needs attribute property" + ); + + expect(response.success).toBeFalsy(); +}); + +test("Check if model is invalid (ONE_TO_MANY)", () => { + model.supply = { + type: Types.ONE_TO_MANY + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch("Model type: supply needs model property."); + + expect(response.message).toMatch( + "Model type: supply needs attribute property" + ); + + expect(response.success).toBeFalsy(); +}); + +test("Check if model is invalid (RADIO)", () => { + model.supply = { + type: Types.RADIO + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch( + "Model type: supply needs options property." + ); + + expect(response.success).toBeFalsy(); +}); + +test("Check if model is invalid (CHECKBOX)", () => { + model.supply = { + type: Types.CHECKBOX + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch( + "Model type: supply needs options property." + ); + + expect(response.success).toBeFalsy(); +}); + +test("Check if model is invalid (SELECT)", () => { + model.supply = { + type: Types.SELECT + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch( + "Model type: supply needs options property." + ); + + expect(response.success).toBeFalsy(); +}); + +test("Check if model is invalid (INVALID TYPE)", () => { + model.supply = { + type: "default" + }; + + let response = Types.modelIsValid(model); + + expect(response.message).toMatch("Model type: supply is invalid."); + + expect(response.success).toBeFalsy(); +}); From 2c216a1769937915bd48eb1e0f65b26d010d9a32 Mon Sep 17 00:00:00 2001 From: "Dion Maicon E. Duarte" Date: Thu, 12 Dec 2019 20:28:22 -0200 Subject: [PATCH 2/3] Add types verification coverage --- js/bootstrap/form.js | 17 +++++--------- js/types.js | 8 +------ test/types.test.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/js/bootstrap/form.js b/js/bootstrap/form.js index 7a2f981..00cc896 100644 --- a/js/bootstrap/form.js +++ b/js/bootstrap/form.js @@ -12,8 +12,7 @@ const Form = class { getTemplate() { let capitalizedName = capitalize(this.modelName); - let template = - `