-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Pisyukaev/core-tests
- Loading branch information
Showing
5 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/xml-locales/src/__snapshots__/xml-locales.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`XmlLocales > XmlLocales constructor > constructor should parse xml data 1`] = ` | ||
XmlJsonData { | ||
"resources": { | ||
"string": [ | ||
{ | ||
"#text": "value1", | ||
"key_name": "key1", | ||
}, | ||
], | ||
}, | ||
} | ||
`; | ||
|
||
exports[`XmlLocales > XmlLocales constructor > should return json object 1`] = ` | ||
XmlJsonData { | ||
"resources": { | ||
"#text": " | ||
", | ||
"string": [ | ||
{ | ||
"#text": "value1", | ||
"key_name": "key1", | ||
}, | ||
{ | ||
"#text": "value2", | ||
"key_name": "key2", | ||
}, | ||
], | ||
}, | ||
} | ||
`; | ||
|
||
exports[`XmlLocales > should return json object 1`] = ` | ||
XmlJsonData { | ||
"resources": { | ||
"#text": " | ||
", | ||
"string": [ | ||
{ | ||
"#text": "value1", | ||
"key_name": "key1", | ||
}, | ||
{ | ||
"#text": "value2", | ||
"key_name": "key2", | ||
}, | ||
], | ||
}, | ||
} | ||
`; | ||
|
||
exports[`XmlLocales > should return xml string 1`] = ` | ||
"<resources> | ||
<string name=\\"key1\\">value1</string> | ||
<string name=\\"key2\\">value2</string> | ||
</resources>" | ||
`; | ||
exports[`XmlLocales > to XML > should return xml string with formatting 1`] = ` | ||
"<resources> | ||
<string name=\\"key1\\">value1</string> | ||
<string name=\\"key2\\">value2</string> | ||
</resources>" | ||
`; | ||
exports[`XmlLocales > to XML > should return xml string without formatting 1`] = ` | ||
"<resources><string name=\\"key1\\">value1</string><string name=\\"key2\\">value2</string> | ||
</resources>" | ||
`; |
15 changes: 15 additions & 0 deletions
15
packages/xml-locales/src/utils/__snapshots__/xml.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`XmlParser > should format xml string 1`] = ` | ||
"<resources> | ||
<string name=\\"key1\\">value1</string> | ||
<string name=\\"key2\\">value2</string> | ||
</resources>" | ||
`; | ||
exports[`XmlParser > should parse json to xml 1`] = ` | ||
"<resources><string name=\\"key1\\">value1</string><string name=\\"key2\\">value2</string> | ||
</resources>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { | ||
checkKeyValueExist, | ||
checkXmlKey, | ||
replaceXmlNodeValue | ||
} from './helpers.js'; | ||
|
||
const xmlData = { | ||
resources: { | ||
string: [ | ||
{ | ||
key_name: 'key1', | ||
'#text': 'value1' | ||
}, | ||
{ | ||
key_name: 'key2', | ||
'#text': 'value2' | ||
} | ||
] | ||
} | ||
}; | ||
|
||
describe('checkXmlKey', () => { | ||
test('should return true if key exist', () => { | ||
expect(checkXmlKey(xmlData, 'key1')).toBe(true); | ||
}); | ||
|
||
test('should return false if key does not exist', () => { | ||
expect(checkXmlKey(xmlData, 'key3')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('checkKeyValueExist', () => { | ||
test('should return true if key value exist', () => { | ||
expect(checkKeyValueExist('value1', xmlData.resources.string)).toBe(true); | ||
}); | ||
|
||
test('should return false if key value does not exist', () => { | ||
expect(checkKeyValueExist('value3', xmlData.resources.string)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('replaceXmlNodeValue', () => { | ||
test('should replace the value of the key', () => { | ||
const newXmlData = replaceXmlNodeValue(xmlData, 'key1', 'value3'); | ||
expect(newXmlData.resources.string[0]?.['#text']).toBe('value3'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { | ||
checkKeyValueExist, | ||
checkXmlKey, | ||
replaceXmlNodeValue | ||
} from './helpers.js'; | ||
import { XmlJsonData, XmlParser } from './xml.js'; | ||
|
||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
`; | ||
|
||
describe('XmlParser', () => { | ||
test('should parse xml to json', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
expect(jsonData).toMatchObject({ | ||
resources: { | ||
string: [ | ||
{ | ||
key_name: 'key1', | ||
'#text': 'value1' | ||
}, | ||
{ | ||
key_name: 'key2', | ||
'#text': 'value2' | ||
} | ||
] | ||
} | ||
}); | ||
}); | ||
|
||
test('should parse json to xml', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
const xmlString = xmlParser.jsonToXml(jsonData); | ||
expect(xmlString).toMatchSnapshot(); | ||
}); | ||
|
||
test('should format xml string', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
const xmlString = xmlParser.jsonToXml(jsonData); | ||
const formattedXmlString = xmlParser.formate(xmlString); | ||
expect(formattedXmlString).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('XmlJsonData', () => { | ||
test('should check if key exist', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
const xmlJsonData = new XmlJsonData(jsonData); | ||
|
||
expect(checkXmlKey(xmlJsonData, 'key1')).toBe(true); | ||
expect(checkXmlKey(xmlJsonData, 'key3')).toBe(false); | ||
}); | ||
|
||
test('should check if key value exist', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
const xmlJsonData = new XmlJsonData(jsonData); | ||
expect(checkKeyValueExist('value1', xmlJsonData.resources.string)).toBe( | ||
true | ||
); | ||
expect(checkKeyValueExist('value3', xmlJsonData.resources.string)).toBe( | ||
false | ||
); | ||
}); | ||
|
||
test('should replace the value of the key', () => { | ||
const xmlParser = new XmlParser(); | ||
const jsonData = xmlParser.xmlToJson(xmlData); | ||
const xmlJsonData = new XmlJsonData(jsonData); | ||
|
||
expect( | ||
checkKeyValueExist( | ||
'value3', | ||
replaceXmlNodeValue(xmlJsonData, 'key1', 'value3').resources.string | ||
) | ||
).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { XmlLocales } from './xml-locales.js'; | ||
|
||
const xmlData = `<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources>`; | ||
|
||
const oneStringXmlData = `<resources> | ||
<string name="key1">value1</string> | ||
</resources>`; | ||
|
||
describe('XmlLocales', () => { | ||
describe('add', () => { | ||
test('should add new node', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
const jsonXml = xmlLocales.add({ key: 'key3', value: 'value3' }).toJSON(); | ||
const newNode = jsonXml.resources.string[2]; | ||
|
||
expect(newNode?.key_name).toBe('key3'); | ||
expect(newNode?.['#text']).toBe('value3'); | ||
}); | ||
|
||
test('should replace the value of the key', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
const jsonXml = xmlLocales.add({ key: 'key1', value: 'value3' }).toJSON(); | ||
const newNode = jsonXml.resources.string[0]; | ||
|
||
expect(newNode?.key_name).toBe('key1'); | ||
expect(newNode?.['#text']).toBe('value3'); | ||
}); | ||
}); | ||
|
||
describe('remove', () => { | ||
test('should remove a node of the key', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.deleteByKey('key1'); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.['#text']).toBe('value2'); | ||
}); | ||
|
||
test('should remove a node of the value', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.deleteByValue('value1'); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.['#text']).toBe('value2'); | ||
}); | ||
|
||
test('not found key or value', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.deleteByKey('key3'); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.['#text']).toBe('value1'); | ||
}); | ||
}); | ||
|
||
describe('sort', () => { | ||
test('should sorted by "asc"', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.sort('asc'); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.key_name).toBe('key1'); | ||
}); | ||
|
||
test('should sorted by "desc"', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.sort('desc'); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[1]?.key_name).toBe('key1'); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
test('should update key', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.update({ oldValue: 'key1', newValue: 'newKey1' }); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.key_name).toBe('newKey1'); | ||
}); | ||
|
||
test('should update value', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
xmlLocales.update({ oldValue: 'value1', newValue: 'newValue1' }); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml.resources.string[0]?.['#text']).toBe('newValue1'); | ||
}); | ||
}); | ||
|
||
describe('XmlLocales constructor', () => { | ||
test('should throw error if xml data is not defined', () => { | ||
expect(() => new XmlLocales()).toThrow('XML data is not defined'); | ||
}); | ||
|
||
test('constructor should parse xml data', () => { | ||
const xmlLocales = new XmlLocales(oneStringXmlData); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml).toMatchSnapshot(); | ||
}); | ||
|
||
test('should return json object', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
const jsonXml = xmlLocales.toJSON(); | ||
|
||
expect(jsonXml).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('to XML', () => { | ||
test('should return xml string with formatting', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
const xmlString = xmlLocales.toXML(); | ||
|
||
expect(xmlString).toMatchSnapshot(); | ||
}); | ||
|
||
test('should return xml string without formatting', () => { | ||
const xmlLocales = new XmlLocales(xmlData); | ||
const xmlString = xmlLocales.toXML(false); | ||
|
||
expect(xmlString).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |