Skip to content

Commit

Permalink
fixes #45 Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
vietredweb committed Apr 6, 2023
1 parent d4d7a69 commit bfa61b1
Show file tree
Hide file tree
Showing 7 changed files with 3,117 additions and 58 deletions.
3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
}
21 changes: 21 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Ensure environment variables are read.
import * as dotenv from 'dotenv';
import fetch from 'node-fetch';
dotenv.config();

export default {
transformIgnorePatterns: ['<rootDir>/node_modules/'],
roots: ['<rootDir>/src'],
testTimeout: 20000,
setupFilesAfterEnv: ['<rootDir>/test/setupTests.js'],
moduleFileExtensions: ['js', 'ts', 'tsx'],
testEnvironment: 'jsdom',
globals: {
'ts-jest': {
babel: true,
// useESM: true,
tsConfig: 'tsconfig.json',
},
fetch,
},
};
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"lint:nowarns": "eslint --quiet \"src/**/\"",
"prepublishOnly": "yarn run build",
"format:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx}'",
"format:write": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'"
"format:write": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'",
"test": "jest"
},
"eslintConfig": {
"extends": [
Expand All @@ -44,15 +45,25 @@
"react-dom": "^17.0.1"
},
"devDependencies": {
"@babel/preset-env": "^7.21.4",
"@babel/preset-typescript": "^7.21.4",
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@types/react": "^18.0.30",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"dotenv": "^16.0.3",
"eslint": "^8.36",
"eslint-plugin-react": "^7.31.10",
"jest": "^29.4.3",
"jest-environment-jsdom": "^29.4.3",
"jest-fetch-mock": "^3.0.3",
"node-fetch": "^3.3.1",
"prettier": "^2.8.4",
"rimraf": "^4.4.1",
"ts-jest": "^29.0.5",
"tsup": "^6.7.0",
"typescript": "^5.0.3"
},
Expand Down
26 changes: 26 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { initTracker, startTracker } from './utils';
import { endTracker } from './utils';

let visitor_uuid = '';
let event_uuid_start = '';
const endPoint = process.env.ENDPOINT_ANALYTICS_URL;

describe('Analytics', () => {
it('Init Tracker', async () => {
const response = await initTracker(endPoint);
visitor_uuid = response?.visitor_uuid;

expect(response).not.toBe(false);
});
it('Start Tracker', async () => {
console.log('visitor_uuidstart,', visitor_uuid);
const response = await startTracker(endPoint, visitor_uuid, '');
event_uuid_start = response?.event_uuid;

expect(response).not.toBe(false);
});
it('End Tracker', async () => {
const response = await endTracker(endPoint, event_uuid_start, visitor_uuid);
expect(response).not.toBe(false);
});
});
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const initTracker = async (
user_agent = window.navigator.userAgent;
const browser = Bowser.parse(window.navigator.userAgent);
const browser_name = browser?.browser?.name;
const browser_version = browser?.browser?.version;
const browser_version = browser?.browser?.version ?? '0';
const lang = window.navigator['userLanguage'] || window.navigator.language;
const device = browser?.platform?.model ?? browser?.platform?.type;
const domain = `${origin}`;
Expand All @@ -31,6 +31,7 @@ const initTracker = async (
urlParams.get(key) && attributes.push({ name: key, value: urlParams.get(key) });
}
}
console.log('referer', referer);
if (!urlParams.get('visitor_uuid')) {
const ip = '';
const response = await trackerService(createRequest(endpoint, 'init'), {
Expand Down
9 changes: 9 additions & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
clear: jest.fn(),
};

global.localStorage = localStorageMock;

global.window = {};
Loading

0 comments on commit bfa61b1

Please sign in to comment.