-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init accelerator lib and topic to share states (#11)
* feat: init accelerator lib and topic to share states * feat: remove console log * feat: delete jest-marbles package * feat: edit read me --------- Co-authored-by: kim.tran <[email protected]>
- Loading branch information
Showing
23 changed files
with
601 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"extends": ["plugin:@nx/angular", "plugin:@angular-eslint/template/process-inline-templates"], | ||
"rules": { | ||
"@angular-eslint/directive-selector": [ | ||
"error", | ||
{ | ||
"type": "attribute", | ||
"prefix": "onecx", | ||
"style": "camelCase" | ||
} | ||
], | ||
"@angular-eslint/component-selector": [ | ||
"error", | ||
{ | ||
"type": "element", | ||
"prefix": "onecx", | ||
"style": "kebab-case" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.html"], | ||
"extends": ["plugin:@nx/angular-template"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "error" | ||
} | ||
} | ||
] | ||
} |
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,7 @@ | ||
# accelerator | ||
|
||
This library contains all framework independent accelerators. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test accelerator` to execute the unit tests. |
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,22 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'accelerator', | ||
preset: '../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
coverageDirectory: '../../coverage/libs/accelerator', | ||
transform: { | ||
'^.+\\.(ts|mjs|js|html)$': [ | ||
'jest-preset-angular', | ||
{ | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
}, | ||
], | ||
}, | ||
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/serializers/no-ng-attributes', | ||
'jest-preset-angular/build/serializers/ng-snapshot', | ||
'jest-preset-angular/build/serializers/html-comment', | ||
], | ||
} |
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,8 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../dist/libs/accelerator", | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
}, | ||
"assets": ["CHANGELOG.md", "./assets/**"] | ||
} |
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,13 @@ | ||
{ | ||
"name": "@onecx/accelerator", | ||
"version": "3.1.1", | ||
"peerDependencies": { | ||
"@angular/common": "^15.2.7", | ||
"@angular/core": "^15.2.7", | ||
"rxjs": "^7.8.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} | ||
|
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,63 @@ | ||
{ | ||
"name": "accelerator", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/accelerator/src", | ||
"prefix": "onecx", | ||
"tags": [], | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/angular:package", | ||
"outputs": [ | ||
"{workspaceRoot}/dist/{projectRoot}" | ||
], | ||
"options": { | ||
"project": "libs/accelerator/ng-package.json" | ||
}, | ||
"configurations": { | ||
"production": { | ||
"tsConfig": "libs/accelerator/tsconfig.lib.prod.json" | ||
}, | ||
"development": { | ||
"tsConfig": "libs/accelerator/tsconfig.lib.json" | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": [ | ||
"{workspaceRoot}/coverage/{projectRoot}" | ||
], | ||
"options": { | ||
"jestConfig": "libs/accelerator/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": [ | ||
"{options.outputFile}" | ||
], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"libs/accelerator/**/*.ts", | ||
"libs/accelerator/**/*.html", | ||
"libs/accelerator/package.json" | ||
] | ||
} | ||
}, | ||
"release": { | ||
"executor": "nx-release:build-update-publish", | ||
"options": { | ||
"libName": "accelerator" | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
export * from './lib/accelerator.module' | ||
export * from './lib/topic/topic' |
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,7 @@ | ||
import { NgModule } from '@angular/core' | ||
import { CommonModule } from '@angular/common' | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
}) | ||
export class AcceleratorModule {} |
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,7 @@ | ||
export class Message { | ||
timestamp: number | ||
|
||
constructor(public type: string) { | ||
this.timestamp = window.performance.now() | ||
} | ||
} |
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,8 @@ | ||
import { TopicMessage } from './topic-message' | ||
import { TopicMessageType } from './topic-message-type' | ||
|
||
export class TopicDataMessage<T> extends TopicMessage { | ||
constructor(type: TopicMessageType, name: string, version: number, public data: T) { | ||
super(type, name, version) | ||
} | ||
} |
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,5 @@ | ||
export const enum TopicMessageType { | ||
TopicNext = 'TopicNext', | ||
TopicGet = 'TopicGet', | ||
} | ||
|
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,12 @@ | ||
import { Message } from "./message"; | ||
import { TopicMessageType } from "./topic-message-type"; | ||
|
||
export class TopicMessage extends Message { | ||
constructor( | ||
type: TopicMessageType, | ||
public name: string, | ||
public version: number | ||
) { | ||
super(type); | ||
} | ||
} |
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,147 @@ | ||
import { map } from 'rxjs' | ||
import { Topic } from './topic' | ||
|
||
describe('Topic', () => { | ||
const origAddEventListener = window.addEventListener | ||
const origPostMessage = window.postMessage | ||
|
||
let listeners: any[] = [] | ||
window.addEventListener = (_type: any, listener: any) => { | ||
listeners.push(listener) | ||
} | ||
|
||
window.removeEventListener = (_type: any, listener: any, ) => { | ||
listeners = listeners.filter((l) => l !== listener) | ||
} | ||
|
||
window.postMessage = (m: any) => { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
listeners.forEach((l) => l({ data: m, stopImmediatePropagation: () => {}, stopPropagation: () => {} })) | ||
} | ||
|
||
afterAll(() => { | ||
window.addEventListener = origAddEventListener | ||
window.postMessage = origPostMessage | ||
}) | ||
|
||
let values1: any[] | ||
let values2: any[] | ||
|
||
let testTopic1: Topic<string> | ||
let testTopic2: Topic<string> | ||
|
||
beforeEach(() => { | ||
listeners = [] | ||
|
||
values1 = [] | ||
values2 = [] | ||
|
||
testTopic1 = new Topic<string>('test', 1) | ||
testTopic2 = new Topic<string>('test', 1) | ||
|
||
testTopic1.subscribe((v) => values1.push(v)) | ||
testTopic2.subscribe((v) => values2.push(v)) | ||
|
||
}) | ||
|
||
it('should have correct value for 2 topics after first topic publishes', () => { | ||
testTopic1.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
}) | ||
|
||
it('should have correct value for 2 topics after second topic publishes', () => { | ||
testTopic2.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
}) | ||
|
||
it('should have same value for a new initialized topic like the already existing topics', () => { | ||
testTopic1.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
|
||
const values3: any[] = [] | ||
const testTopic3 = new Topic<string>('test', 1) | ||
testTopic3.subscribe((v) => values3.push(v)) | ||
|
||
expect(values3).toEqual(['value1']) | ||
}) | ||
|
||
it('should have same values for both topics after one topic publishes 2 values', () => { | ||
testTopic1.publish('value1') | ||
testTopic2.publish('value2') | ||
|
||
expect(values1).toEqual(['value1', 'value2']) | ||
expect(values2).toEqual(['value1', 'value2']) | ||
}) | ||
|
||
it('should has no value if message name is different', () => { | ||
testTopic1.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
|
||
const values3: any[] = [] | ||
const testTopic3 = new Topic<string>('test123', 1) | ||
testTopic3.subscribe((v) => values3.push(v)) | ||
|
||
expect(values3).toEqual([]) | ||
}) | ||
|
||
it('should has no value if message version is different', () => { | ||
testTopic1.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
|
||
const values3: any[] = [] | ||
const testTopic3 = new Topic<string>('test', 2) | ||
testTopic3.subscribe((v) => values3.push(v)) | ||
|
||
expect(values3).toEqual([]) | ||
}) | ||
|
||
it('should has no value if message is undefined', () => { | ||
testTopic1.publish('value1') | ||
|
||
expect(values1).toEqual(['value1']) | ||
expect(values2).toEqual(['value1']) | ||
|
||
const values3: any[] = [] | ||
const testTopic3 = new Topic<undefined>('', 0) | ||
testTopic3.subscribe((v) => values3.push(v)) | ||
testTopic3.publish(undefined) | ||
|
||
expect(values3).toEqual([]) | ||
}) | ||
|
||
it('should get correct value', () => { | ||
expect(testTopic1.getValue()).toEqual(undefined) | ||
|
||
testTopic1.publish('value1') | ||
|
||
expect(testTopic1.getValue()).toEqual('value1') | ||
expect(testTopic2.getValue()).toEqual('value1') | ||
}) | ||
|
||
it('should remove event listener', () => { | ||
testTopic1.destroy() | ||
testTopic2.publish('value1') | ||
|
||
expect(values1).toEqual([]) | ||
expect(values2).toEqual(['value1']) | ||
}) | ||
|
||
it('should pipe to get the length of the value', () => { | ||
let v = 0 | ||
testTopic1.pipe(map((v) => v.length)).subscribe((s) => v = s) | ||
testTopic1.publish('value1') | ||
|
||
expect(v).toEqual(6) | ||
expect(values1).toEqual(['value1']) | ||
}) | ||
}) |
Oops, something went wrong.