Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# dependencies
/node_modules

# Jest
/coverage

# JS
index.js

# production
/dist

Expand Down
102 changes: 102 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import umami, { UmamiOptions, UmamiPayload } from './index';

const mockFetchResponse = {
ok: true,
json: jest.fn().mockResolvedValue({ success: true }),
};

// Helper function to mock fetch
const mockFetch = () => {
global.fetch = jest.fn().mockResolvedValue(mockFetchResponse);
};

// Helper function to run common tests
const runCommonTests = () => {
test('should initialize with default options', () => {
expect(umami.options.websiteId).toBe('test-website');
expect(umami.options.hostUrl).toBe('https://example.com');
});

test('should track events', async () => {
const eventName = 'page_view';
const eventData = { screen: '1920x1080' };

mockFetch();
const response = await umami.track(eventName, eventData);
expect(fetch).toHaveBeenCalled();
expect(response.ok).toBe(true);
});

test('should track events with payload', async () => {
const event: UmamiPayload = { title: 'test', website: 'test' };
const eventData = { screen: '1920x1080' };

mockFetch();
const response = await umami.track(event, eventData);
expect(fetch).toHaveBeenCalled();
expect(response.ok).toBe(true);
});

test('should error on bad payload', async () => {
const event = undefined as unknown as UmamiPayload;
const eventData = { screen: '1920x1080' };

mockFetch();
try {
await umami.track(event, eventData);
} catch (error) {
expect(error).toEqual("Invalid payload.");
}

});

test('should identify user', async () => {
const properties = { userId: 'user123' };

mockFetch();
const response = await umami.identify(properties);
expect(fetch).toHaveBeenCalled();
expect(response.ok).toBe(true);
});

test('should identify user with no inputs', async () => {
mockFetch();
const response = await umami.identify();
expect(fetch).toHaveBeenCalled();
expect(response.ok).toBe(true);
});

test('should reset properties', () => {
umami.reset();
expect(umami.properties).toEqual({});
});
};

describe('Umami', () => {
const options: UmamiOptions = {
websiteId: 'test-website',
hostUrl: 'https://example.com',
};

beforeEach(() => {
umami.reset();
umami.init(options);
});

runCommonTests();
});

describe('Umami with user agent', () => {
const options: UmamiOptions = {
websiteId: 'test-website',
hostUrl: 'https://example.com',
userAgent: 'Mozilla',
};

beforeEach(() => {
umami.reset();
umami.init(options);
});

runCommonTests();
});
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export class Umami {
switch (type) {
case 'string':
return this.send({
website: websiteId,
website: websiteId as string,
name: event as string,
data: eventData,
});
case 'object':
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.send({ website: websiteId, ...(event as UmamiPayload) });
}

Expand All @@ -73,7 +75,7 @@ export class Umami {
const { websiteId, sessionId } = this.options;

return this.send(
{ website: websiteId, session: sessionId, data: { ...this.properties } },
{ website: websiteId as string, session: sessionId, data: { ...this.properties } },
'identify',
);
}
Expand All @@ -85,4 +87,4 @@ export class Umami {

const umami = new Umami();

export default umami;
export default umami;
13 changes: 13 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Config} from 'jest';

const config: Config = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['html', 'text'],
};

export default config;
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umami/node",
"version": "0.4.0",
"version": "0.5.0",
"description": "Node client for Umami",
"keywords": [
"node",
Expand All @@ -27,7 +27,8 @@
"build": "rollup -c",
"types": "tsc && tsc-alias",
"check-types": "tsc",
"lint": "eslint src --ext .ts,.tsx"
"lint": "eslint . --ext .ts,.tsx",
"test": "jest"
},
"lint-staged": {
"src/**/*.{js,ts}": [
Expand All @@ -39,6 +40,7 @@
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-node-resolve": "^15.0.1",
"@testing-library/react": "^12.1.1",
"@types/jest": "^29.5.14",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
Expand All @@ -54,7 +56,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-storybook": "^0.6.12",
"husky": "^7.0.4",
"jest": "^27.2.4",
"jest": "^29.7.0",
"lint-staged": "^11.1.2",
"prettier": "^2.2.1",
"prettier-eslint": "^13.0.0",
Expand All @@ -63,6 +65,8 @@
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-esbuild": "^6.1.0",
"rollup-plugin-postcss": "^4.0.2",
"ts-jest": "^29.2.6",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.2",
"typescript": "^4.7.4"
}
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"resolveJsonModule": true, /* Enable importing .json files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
Loading