Skip to content

Commit 6d11090

Browse files
authored
NEW: MultiLangString stringifies as Object (#38)
closes #38
1 parent a313295 commit 6d11090

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

src/models/MultiLangString.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class MultiLangString extends Map {
103103
super(Object.entries(data));
104104

105105
}
106+
107+
toJSON() {
108+
return Object.fromEntries(this);
109+
}
110+
106111
}
107112

108113
export default new Proxy(MultiLangString, classTraps);

src/models/MultiLangString.test.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const {
99

1010
const modelName = `MultiLangString`;
1111

12+
const sampleData = { eng: 'Hello world!', spa: 'Hola mundo!' };
13+
1214
describe(modelName, () => {
1315

1416
it(`class: MultiLangString`, () => {
@@ -70,28 +72,36 @@ describe(modelName, () => {
7072

7173
describe(`MultiLangString.prototype.{language}`, () => {
7274

73-
const data = { eng: 'Hello world!', spa: 'Hola mundo!' };
74-
7575
it(`Instantiation`, () => {
76-
const mls = new MultiLangString(data);
77-
expect(mls.get(`eng`)).toBe(data.eng);
78-
expect(mls.get(`spa`)).toBe(data.spa);
76+
const mls = new MultiLangString(sampleData);
77+
expect(mls.get(`eng`)).toBe(sampleData.eng);
78+
expect(mls.get(`spa`)).toBe(sampleData.spa);
7979
});
8080

8181
it(`Error: set bad language tag`, () => {
82-
const mls = new MultiLangString(data);
82+
const mls = new MultiLangString(sampleData);
8383
const setBadLanguageTag = () => mls.set(`Tlahuapa Mixtec`, `ayoo`);
8484
expect(setBadLanguageTag).toThrowMatching(e => e.name === `LanguageTagError`);
8585
});
8686

8787
it(`Error: set bad string`, () => {
88-
const mls = new MultiLangString(data);
88+
const mls = new MultiLangString(sampleData);
8989
const setBadString = () => mls.set(`mix`, true);
9090
expect(setBadString).toThrowMatching(e => e.name === `MultiLangStringError`);
9191
});
9292

9393
});
9494

95+
it(`MultiLangString.prototype.toJSON()`, () => {
96+
97+
const mls = new MultiLangString(sampleData);
98+
const pojo = JSON.parse(JSON.stringify(mls));
99+
100+
Object.keys(sampleData)
101+
.forEach(key => expect(pojo[key]).toBe(sampleData[key]));
102+
103+
});
104+
95105
it(`~~MultiLangString.prototype.type~~`, () => {
96106
expect(new MultiLangString().type).toBeUndefined();
97107
});

src/models/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
/* eslint-disable
2+
max-nested-callbacks,
3+
*/
4+
15
const { models } = require(`../../test`);
26

37
const {
48
MultiLangString,
59
Language,
610
} = models;
711

8-
/**
9-
* Check that the models module has the expected exports
10-
*/
11-
1212
describe(`models`, () => {
1313

1414
it(`has the expected exports`, () => {

0 commit comments

Comments
 (0)