From 622314fe37336129d01c4581d62ce8cc1d2543be Mon Sep 17 00:00:00 2001 From: "Daniel W. Hieber" Date: Thu, 27 Aug 2020 14:33:17 -0500 Subject: [PATCH] Language.type --- src/core/Model.js | 28 +++++++++--------- src/models/Language.js | 2 ++ src/models/Language.test.js | 58 +++++++++---------------------------- 3 files changed, 29 insertions(+), 59 deletions(-) diff --git a/src/core/Model.js b/src/core/Model.js index 25ef59f..044500f 100644 --- a/src/core/Model.js +++ b/src/core/Model.js @@ -54,6 +54,20 @@ class Model { } + /** + * Create a "type" property on an object that is non-writable + * @param {Object} object The object to define the property on + * @param {String} type The value to use for the type + */ + static defineTypeProp(object, type) { + Object.defineProperty(object, `type`, { + configurable: true, + enumerable: true, + value: type, + writable: false, + }); + } + /** * A utility function that defines a property on a class that validates its value when set. * @param {Object} object [description] @@ -79,20 +93,6 @@ class Model { } - /** - * Create a "type" property on an object that is non-writable - * @param {Object} object The object to define the property on - * @param {String} type The value to use for the type - */ - static defineTypeProp(object, type) { - Object.defineProperty(object, `type`, { - configurable: true, - enumerable: true, - value: type, - writable: false, - }); - } - } export default Model; diff --git a/src/models/Language.js b/src/models/Language.js index a7c82d8..87e1614 100644 --- a/src/models/Language.js +++ b/src/models/Language.js @@ -54,6 +54,7 @@ function validateISOCode(input) { * @prop {String} glottolog - The Glottocode for this language * @prop {String} iso - The ISO 639-3 code for this language * @prop {models.MultiLangString} name - The name of this language + * @prop {String} type - "Language" */ class Language extends Model { @@ -72,6 +73,7 @@ class Language extends Model { // Property Definitions Model.defineModelProp(this, `name`, MultiLangString); + Model.defineTypeProp(this, `Language`); Model.defineValidatedProp(this, `abbreviation`, validateAbbreviation); Model.defineValidatedProp(this, `glottolog`, validateGlottoCode); Model.defineValidatedProp(this, `iso`, validateISOCode); diff --git a/src/models/Language.test.js b/src/models/Language.test.js index 3c987ab..261b2fe 100644 --- a/src/models/Language.test.js +++ b/src/models/Language.test.js @@ -13,7 +13,7 @@ describe(`Language`, () => { Language.name.should.equal(`Language`); }); - it(`Abbreviation`, () => { + it(`abbreviation`, () => { const lang = new Language; (() => { lang.abbreviation = undefined; }).should.not.throw(); (() => { lang.abbreviation = `ctm`; }).should.not.throw(); @@ -21,13 +21,13 @@ describe(`Language`, () => { (typeof lang.abbreviation).should.not.equal(`object`); }); - it(`Custom Property`, () => { + it(`custom property`, () => { const lang = new Language; (() => { lang.deleted = true; }).should.not.throw(); lang.deleted.should.equal(true); }); - it(`Glottocode`, () => { + it(`glottolog`, () => { const lang = new Language; (() => { lang.glottolog = undefined; }).should.not.throw(); (() => { lang.glottolog = `stan1293`; }).should.not.throw(); @@ -35,7 +35,7 @@ describe(`Language`, () => { (typeof lang.glottolog).should.not.equal(`object`); }); - it(`ISO 639-3 code`, () => { + it(`iso`, () => { const lang = new Language; (() => { lang.iso = undefined; }).should.not.throw(); (() => { lang.iso = `ctm`; }).should.not.throw(); @@ -43,51 +43,19 @@ describe(`Language`, () => { (typeof lang.iso).should.not.equal(`object`); }); - describe(`name`, () => { + it(`name`, () => { - it(`class: MultiLangString`, () => { - const lang = new Language(); - lang.name.should.be.instanceOf(MultiLangString); - }); + const lang = new Language({ name: { eng: `Chitimacha` } }); - it(`enumerable`, () => { - const lang = new Language({ name: 'Chitimacha' }); - Object.keys(lang).should.contain(`name`); - }); + lang.name.should.be.instanceOf(MultiLangString); + lang.name.get(`eng`).should.equal(`Chitimacha`); - it(`Success: String`, () => { - - const name = `Chitimacha`; - const lang = new Language; - - lang.name = name; - - lang.name.get(`eng`).should.equal(name); - - }); - - it(`Success: Object`, () => { - - const name = { - eng: `Chitimacha`, - fra: `chitimacha`, - }; - - const lang = new Language; - - lang.name = name; - - lang.name.get(`eng`).should.equal(name.eng); - lang.name.get(`fra`).should.equal(name.fra); - - }); - - it(`Error: bad data`, () => { - const lang = new Language; - const setBadLang = () => { lang.name = false; }; - setBadLang.should.throw().with.property(`name`, `MultiLangStringDataError`); - }); + }); + it(`type`, () => { + const lang = new Language; + lang.type.should.equal(`Language`); + (() => { lang.type = `language`; }).should.throw(); }); });