-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance construct elementString (#103)
* feat: implement concat service that can apply to the identifierKeyPath config Signed-off-by: Nam Hoang <[email protected]> * test: add test for adding services Signed-off-by: Nam Hoang <[email protected]> * chore: remove comment Signed-off-by: Nam Hoang <[email protected]> * refactor: handle elementString missing AI code Signed-off-by: Nam Hoang <[email protected]> * feat: remove the local storage used data for the process DPP Signed-off-by: Nam Hoang <[email protected]> * docs: add document for identifierKeyPath Signed-off-by: Nam Hoang <[email protected]> * refactor: handle exception case for construct identifier Signed-off-by: Nam Hoang <[email protected]> * test: add test for local storage service Signed-off-by: Nam Hoang <[email protected]> --------- Signed-off-by: Nam Hoang <[email protected]>
- Loading branch information
1 parent
0a0d949
commit 2ab5d12
Showing
15 changed files
with
537 additions
and
57 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
documentation/docs/mock-apps/common/identifier-key-path.md
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,45 @@ | ||
--- | ||
sidebar_position: 40 | ||
title: Identify Key Path | ||
--- | ||
|
||
import Disclaimer from '../.././\_disclaimer.mdx'; | ||
|
||
<Disclaimer /> | ||
|
||
## Description | ||
|
||
The `IdentifierKeyPath` is a property of services that interact with the data issued to get the identifier to be used for the [IDR](/docs/mock-apps/common/idr) registration. It can be a JSON path of the identifier of the data issued or an object that contains the function `concatService` and the `args` to be used to get the identifier. | ||
|
||
## Example | ||
|
||
```json | ||
{ | ||
"identifierKeyPath": "/eventID" | ||
} | ||
``` | ||
|
||
or | ||
|
||
```json | ||
{ | ||
"identifierKeyPath": { | ||
"function": "concatService", | ||
"args": [ | ||
{ "type": "text", "value": "(01)" }, | ||
{ "type": "path", "value": "/productIdentifier/0/identifierValue" }, | ||
{ "type": "text", "value": "(10)" }, | ||
{ "type": "path", "value": "/batchIdentifier/0/identifierValue" }, | ||
{ "type": "text", "value": "(21)" }, | ||
{ "type": "path", "value": "/itemIdentifier/0/identifierValue" } | ||
] | ||
} | ||
} | ||
``` | ||
|
||
## Definition for object | ||
|
||
| Property | Required | Description | Type | | ||
| -------- | :------: | ------------------------------------------------ | ------ | | ||
| function | Yes | The concat function supported | String | | ||
| args | Yes | The array of object that can be `text` or `path` | Array | |
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
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
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
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
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
139 changes: 139 additions & 0 deletions
139
packages/services/src/__tests__/features/localStorage.service.test.ts
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,139 @@ | ||
import { | ||
deleteItemFromLocalStorage, | ||
deleteValuesFromLocalStorage, | ||
mergeToLocalStorage, | ||
saveToLocalStorage, | ||
} from '../../features/localStorage.service'; | ||
|
||
describe('saveToLocalStorage', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should save the data to local storage', () => { | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key' }; | ||
saveToLocalStorage(data, parameters); | ||
expect(spySetItem).toHaveBeenCalledWith('key', JSON.stringify(data)); | ||
}); | ||
|
||
it('should throw an error if an error occurs', () => { | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key' }; | ||
const error = new Error('An error occurred'); | ||
spySetItem.mockImplementationOnce(() => { | ||
throw error; | ||
}); | ||
expect(() => saveToLocalStorage(data, parameters)).toThrow(error); | ||
}); | ||
}); | ||
|
||
describe('mergeToLocalStorage', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should merge the data to local storage when not existing', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key', objectKeyPath: '/a' }; | ||
spyGetItem.mockReturnValueOnce(JSON.stringify(null)); | ||
mergeToLocalStorage(data, parameters); | ||
expect(spySetItem).toHaveBeenCalledWith('key', JSON.stringify({ 1: { a: 1 } })); | ||
}); | ||
|
||
it('should merge the data to local storage when existing', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key', objectKeyPath: '/a' }; | ||
spyGetItem.mockReturnValueOnce(JSON.stringify({ 2: { a: 2 } })); | ||
mergeToLocalStorage(data, parameters); | ||
expect(spySetItem).toHaveBeenCalledWith('key', JSON.stringify({ 2: { a: 2 }, 1: { a: 1 } })); | ||
}); | ||
|
||
it('should throw error when invalid objectKeyPath is provided', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key', objectKeyPath: 'invalid' }; | ||
spyGetItem.mockReturnValueOnce(JSON.stringify({ a: 2 })); | ||
expect(() => mergeToLocalStorage(data, parameters)).toThrow(); | ||
}); | ||
|
||
it('should throw an error if an error occurs', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1 }; | ||
const parameters = { storageKey: 'key', objectKeyPath: '/a' }; | ||
const error = new Error('An error occurred'); | ||
spyGetItem.mockReturnValueOnce(JSON.stringify({ a: 2 })); | ||
spySetItem.mockImplementationOnce(() => { | ||
throw error; | ||
}); | ||
expect(() => mergeToLocalStorage(data, parameters)).toThrow(error); | ||
}); | ||
}); | ||
|
||
describe('deleteValuesFromLocalStorage', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should delete the values from local storage', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1, b: 2 }; | ||
const parameters = { storageKey: 'key', keys: ['a'] }; | ||
spyGetItem.mockReturnValueOnce(JSON.stringify(data)); | ||
deleteValuesFromLocalStorage(parameters); | ||
expect(spySetItem).toHaveBeenCalledWith('key', JSON.stringify({ b: 2 })); | ||
}); | ||
|
||
it('should not delete the values from local storage if the key does not exist', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const parameters = { storageKey: 'key', keys: ['a'] }; | ||
spyGetItem.mockReturnValueOnce(JSON.stringify({ b: 2 })); | ||
deleteValuesFromLocalStorage(parameters); | ||
expect(spySetItem).toHaveBeenCalledWith('key', JSON.stringify({ b: 2 })); | ||
}); | ||
|
||
it('should throw an error if an error occurs', () => { | ||
const spyGetItem = jest.spyOn(Storage.prototype, 'getItem'); | ||
const spySetItem = jest.spyOn(Storage.prototype, 'setItem'); | ||
const data = { a: 1, b: 2 }; | ||
const parameters = { storageKey: 'key', keys: ['a'] }; | ||
const error = new Error('An error occurred'); | ||
spyGetItem.mockReturnValueOnce(JSON.stringify(data)); | ||
spySetItem.mockImplementationOnce(() => { | ||
throw error; | ||
}); | ||
expect(() => deleteValuesFromLocalStorage(parameters)).toThrow(error); | ||
}); | ||
}); | ||
|
||
describe('deleteItemFromLocalStorage', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should delete the item from local storage', () => { | ||
const spyRemoveItem = jest.spyOn(Storage.prototype, 'removeItem'); | ||
const parameters = { storageKey: 'key' }; | ||
deleteItemFromLocalStorage(parameters); | ||
expect(spyRemoveItem).toHaveBeenCalledWith('key'); | ||
}); | ||
|
||
it('should throw an error if an error occurs', () => { | ||
const spyRemoveItem = jest.spyOn(Storage.prototype, 'removeItem'); | ||
const parameters = { storageKey: 'key' }; | ||
const error = new Error('An error occurred'); | ||
spyRemoveItem.mockImplementationOnce(() => { | ||
throw error; | ||
}); | ||
expect(() => deleteItemFromLocalStorage(parameters)).toThrow(error); | ||
}); | ||
}); |
Oops, something went wrong.