-
Notifications
You must be signed in to change notification settings - Fork 6
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 #766 from Availity/EB-719-life
Adding package to support user activity going forward
- Loading branch information
Showing
11 changed files
with
271 additions
and
0 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,23 @@ | ||
# Changelog | ||
|
||
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). | ||
|
||
# 0.1.0-alpha.0 (2024-10-22) | ||
|
||
|
||
### Features | ||
|
||
* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) | ||
* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) | ||
* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) | ||
|
||
|
||
|
||
# 0.1.0-alpha.0 (2024-10-22) | ||
|
||
|
||
### Features | ||
|
||
* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) | ||
* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) | ||
* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) |
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,25 @@ | ||
# @availity/user-activity-broadcaster | ||
|
||
> This package broadcasts user activity to the navigation. | ||
[![Version](https://img.shields.io/npm/v/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) | ||
[![Dependecy Status](https://img.shields.io/librariesio/release/npm/@availity/user-activity-broadcaster?style=for-the-badge)](https://github.com/Availity/sdk-js/blob/master/packages/user-activity-broadcaster/package.json) | ||
|
||
## Installation | ||
|
||
### NPM | ||
|
||
```bash | ||
npm install @availity/user-activity-broadcaster | ||
``` | ||
|
||
### Yarn | ||
|
||
```bash | ||
yarn add @availity/user-activity-broadcaster | ||
``` | ||
|
||
## Usage | ||
|
||
> All you have to do is include this as a dependency and import it into the root of your web application. |
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 @@ | ||
const global = require('../../jest.config'); | ||
|
||
module.exports = { | ||
...global, | ||
displayName: 'user-activity-broadcaster', | ||
coverageDirectory: '../../coverage/user-activity-broadcaster', | ||
}; |
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,26 @@ | ||
{ | ||
"name": "@availity/user-activity-broadcaster", | ||
"version": "0.1.0-alpha.0", | ||
"description": "This package broacasts user activity to the navigation.", | ||
"main": "src/user-activity-broadcaster.js", | ||
"keywords": [], | ||
"author": "Joe Spanbauer <joseph.spanbauer@availity.com>", | ||
"engines": { | ||
"node": "^18.0.0 || ^20.0.0" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.js --format esm,cjs --dts", | ||
"dev": "tsup src/index.js --format esm,cjs --watch --dts", | ||
"lint": "eslint src", | ||
"lint:fix": "eslint src --fix", | ||
"clean": "rm -rf node_modules && rm -rf dist", | ||
"publish": "yarn npm publish --tolerate-republish --access public" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tsup": "^7.2.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,39 @@ | ||
{ | ||
"name": "@availity/user-activity-broadcaster", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"projectType": "library", | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/resolve-url"], | ||
"options": { | ||
"jestConfig": "packages/user-activity-broadcaster/jest.config.js" | ||
} | ||
}, | ||
"version": { | ||
"executor": "@jscutlery/semver:version", | ||
"options": { | ||
"preset": "angular", | ||
"commitMessageFormat": "chore(user-activity-broadcaster): release version ${version} [skip ci]", | ||
"tagPrefix": "{projectName}@", | ||
"baseBranch": "master", | ||
"trackDeps": true | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"options": { | ||
"eslintConfig": ".eslintrc.yaml", | ||
"silent": false, | ||
"fix": false, | ||
"cache": true, | ||
"cacheLocation": "./node_modules/.cache/user-activity-broadcaster/.eslintcache", | ||
"maxWarnings": -1, | ||
"quiet": false, | ||
"noEslintrc": false, | ||
"hasTypeAwareRules": true, | ||
"cacheStrategy": "metadata" | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
/* eslint-disable unicorn/no-empty-file */ |
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,51 @@ | ||
const MINUTE = 60 * 1000 | ||
const interval = MINUTE * 5 | ||
export const eventName = 'user_activity' | ||
|
||
// Must be a dynamic object to test this | ||
export const lastActivity = {} | ||
|
||
export const getTargetOrigin = (origin = window.location.origin) => { | ||
// Setup targetOrigin to alternate origin (because the same origin already works) | ||
if (origin) { | ||
if (origin.includes('apps')) { | ||
return origin.replace('apps', 'essentials') | ||
} | ||
|
||
if (origin.includes('essentials')) { | ||
return origin.replace('essentials', 'apps') | ||
} | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
const targetOrigin = getTargetOrigin() | ||
|
||
// PostMessage Logic | ||
export const handleActivityUpdate = () => { | ||
window.top.postMessage({ | ||
event: eventName, | ||
time: lastActivity.time | ||
}, targetOrigin) | ||
} | ||
|
||
// Debounce Logic | ||
let activityIntervalId = setInterval(handleActivityUpdate, interval) | ||
// Re-assignable for testing | ||
export const updateInterval = (newInterval) => { | ||
clearInterval(activityIntervalId) | ||
activityIntervalId = setInterval(handleActivityUpdate, newInterval) | ||
} | ||
|
||
// Event Handlers | ||
export const handleActivity = () => { | ||
lastActivity.time = Date.now().toString() | ||
} | ||
|
||
// Add ability to test handleActivity and events | ||
export const addEventListeners = () => { | ||
document.addEventListener('mousedown', handleActivity) | ||
document.addEventListener('keydown', handleActivity) | ||
} | ||
addEventListeners() |
76 changes: 76 additions & 0 deletions
76
packages/user-activity-broadcaster/src/tests/index.test.js
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,76 @@ | ||
const userActivityBroadcaster = require('../index') | ||
|
||
const { | ||
getTargetOrigin, | ||
handleActivity, | ||
updateInterval, | ||
addEventListeners, | ||
lastActivity | ||
} = userActivityBroadcaster | ||
|
||
describe('user-activity-broadcaster', () => { | ||
describe('targetOrigin', () => { | ||
test('essentials.availity.com origin should have targetOrigion of apps', () => { | ||
const testOrigin = 'essentials.availity.com' | ||
const expected = 'apps.availity.com' | ||
|
||
const targetOrigin = getTargetOrigin(testOrigin) | ||
|
||
expect(targetOrigin).toBe(expected) | ||
}) | ||
|
||
test('apps.availity.com origin should have targetOrigion of essentials', () => { | ||
const testOrigin = 'apps.availity.com' | ||
const expected = 'essentials.availity.com' | ||
|
||
const targetOrigin = getTargetOrigin(testOrigin) | ||
|
||
expect(targetOrigin).toBe(expected) | ||
}) | ||
}) | ||
|
||
test('should call handleActivityUpdate every interval', async () => { | ||
const testInterval = 1000 | ||
const waitTime = 2999 | ||
const expectedCallCount = 2 | ||
|
||
window.top.postMessage = () => {} | ||
|
||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivityUpdate') | ||
|
||
updateInterval(testInterval) | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve() | ||
}, waitTime) | ||
}) | ||
|
||
expect(spy).toHaveBeenCalledTimes(expectedCallCount) | ||
}) | ||
|
||
test('keydown events call handleActivity', async () => { | ||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') | ||
|
||
addEventListeners() | ||
|
||
document.dispatchEvent(new MouseEvent('keydown')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
}) | ||
|
||
test('mousedown events call handleActivity', async () => { | ||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') | ||
|
||
addEventListeners() | ||
|
||
document.dispatchEvent(new MouseEvent('mousedown')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
}) | ||
|
||
test('handleActivity should update handleActivity', () => { | ||
handleActivity() | ||
expect(lastActivity.time).toBeDefined() | ||
}) | ||
}) |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"], | ||
"allowJs": true | ||
}, | ||
"include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.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