forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ccdaservice): improve DataStack class (openemr#6718)
* chore(deps): add jest as dev dependency * chore(deps): update package-lock.json * feat(ccdaservice): encapsulate DataStack * test(ccdaservice): create tests for DataStack class * refactor(ccdaservice): import and use DataStack from class file * chore(deps): pin jest version * refactor(ci): improve lint:js-fix script * chore(eslint): support for jest * chore(ci): create step for JS unit tests * chore(deps): pin package versions
- Loading branch information
1 parent
54ed6be
commit 787c3da
Showing
9 changed files
with
8,391 additions
and
4,997 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: JS Unit Test | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
|
||
js_unit_test: | ||
runs-on: ubuntu-22.04 | ||
name: JS Unit Test | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Install npm package | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18' | ||
|
||
- name: npm install | ||
run: | | ||
failTest=false | ||
npm install || failTest=true | ||
if $failTest; then | ||
exit 1 | ||
fi | ||
- name: Run Unit Tests | ||
run: | | ||
failTest=false | ||
npm run test:js || failTest=true | ||
if $failTest; then | ||
exit 1 | ||
fi |
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 @@ | ||
'use strict'; | ||
|
||
class DataStack { | ||
#buffer; | ||
#delimiter; | ||
|
||
constructor(delimiter) { | ||
this.#delimiter = delimiter; | ||
this.#buffer = ''; | ||
} | ||
|
||
endOfCcda() { | ||
return this.#buffer.length === 0 || this.#buffer.indexOf(this.#delimiter) === -1; | ||
} | ||
|
||
push(data) { | ||
this.#buffer += data; | ||
} | ||
|
||
#fetchBuffer() { | ||
const delimiterIndex = this.#buffer.indexOf(this.#delimiter); | ||
if (delimiterIndex === -1) return null; | ||
const data = this.#buffer.slice(0, delimiterIndex); | ||
this.#buffer = this.#buffer.replace(data + this.#delimiter, ''); | ||
return data; | ||
} | ||
|
||
returnData() { | ||
return this.#fetchBuffer(); | ||
} | ||
|
||
clear() { | ||
this.#buffer = ''; | ||
} | ||
|
||
readStackByDelimiter(delimiter) { | ||
const originalDelimiter = this.#delimiter; | ||
this.#delimiter = delimiter; | ||
const message = this.#fetchBuffer(); | ||
this.#delimiter = originalDelimiter; | ||
return message; | ||
} | ||
} | ||
|
||
exports.DataStack = DataStack; |
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,60 @@ | ||
const DataStack = require('./data-stack.js').DataStack; | ||
|
||
describe('DataStack', () => { | ||
const delimiter = ';' | ||
let stack; | ||
|
||
beforeEach(() => { | ||
stack = new DataStack(delimiter); | ||
}); | ||
|
||
it('should create a new stack', () => { | ||
expect(stack).toBeTruthy(); | ||
}); | ||
|
||
it('should store and return data', () => { | ||
const hello = 'Hello'; | ||
const world = 'World!'; | ||
stack.push(hello + delimiter); | ||
stack.push(world + delimiter); | ||
expect(stack.returnData()).toEqual(hello); | ||
expect(stack.returnData()).toEqual(world); | ||
}); | ||
|
||
describe('endOfCcda', () => { | ||
it('should return true if there is no data in the stack', () => { | ||
expect(stack.endOfCcda()).toEqual(true); | ||
}); | ||
|
||
it('should return true if the delimiter is not part of the data', () => { | ||
stack.push('Undelimited data'); | ||
expect(stack.endOfCcda()).toEqual(true); | ||
}); | ||
|
||
it('should return false otherwise', () => { | ||
stack.push(`OpenEMR${delimiter}`); | ||
expect(stack.endOfCcda()).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('clear', () => { | ||
it('should remove all data from the stack', () => { | ||
stack.push(`OpenEMR${delimiter}`); | ||
stack.clear(); | ||
expect(stack.endOfCcda()).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('readStackByDelimiter', () => { | ||
beforeEach(() => stack.push(`OpenEMR!The best app${delimiter}`)); | ||
|
||
it('should be able to read the stack using a different delimiter', () => { | ||
expect(stack.readStackByDelimiter('!')).toEqual('OpenEMR'); | ||
}); | ||
|
||
it('should preserve the original delimiter for future readings', () => { | ||
stack.readStackByDelimiter('!'); | ||
expect(stack.returnData()).toEqual('The best app'); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import('jest').Config} */ | ||
const config = { | ||
modulePathIgnorePatterns: [ | ||
'public/assets', | ||
'vendor' | ||
] | ||
}; | ||
|
||
module.exports = config; |
Oops, something went wrong.