Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Bereket Engida committed May 25, 2023
1 parent 78b72cc commit 903dfe2
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
Binary file modified packages/adapter-prisma/prisma/dev.db
Binary file not shown.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "./dist/index.js",
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"build": "tsup",
"dev": "tsup --watch"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const internalRouter = async (req: ApiRequest<any, any>, options: LogLibO
}
const method = req.method.toUpperCase() as "POST" | "PUT" | "DELETE" | "GET"
let path = ""
if (method === "POST") {
if (method === "POST" || method === "PUT" || method === "DELETE") {
if (typeof req.body !== 'object' || Array.isArray(req.body)) {
return { message: "Invalid request body. Expected an object.", code: 400 }
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/router/routes/event/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const postEvent: ApiPostHandler<EventPostInput> = async (req, options) =>
code: 200,
data
}
} catch {
throw new GenericError('Error creating event', { path: "/event" })
} catch (e) {
throw new GenericError('Error creating event', { path: "/event", e })
}
} else {
throw new GenericError('Invalid request body', { path: "/event" })
Expand Down
10 changes: 4 additions & 6 deletions packages/core/test/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { eventPost, EventPostInput } from '../src/router/routes/event/post';
import { postEvent, EventPostInput } from '../src/router/routes/event/post';
import { Adapter, Events, GenericError } from '../src';


describe('eventPost', () => {
const mockAdapter = {
tracker: {
createManyEvents: vi.fn(),
},
createManyEvents: vi.fn(),
} as unknown as Adapter;

beforeEach(() => {
Expand Down Expand Up @@ -49,7 +47,7 @@ describe('eventPost', () => {
code: 200,
data: expectedData,
};
const response = await eventPost({ body: input, headers: {} }, { adapter: mockAdapter });
const response = await postEvent({ body: input, headers: {} }, { adapter: mockAdapter });
expect(mockAdapter.createManyEvents).toHaveBeenCalledWith(expectedData);
expect(response).toEqual(expectedResponse);
});
Expand All @@ -66,7 +64,7 @@ describe('eventPost', () => {
},
],
} as EventPostInput;
await expect(eventPost({ body: input, headers: {} }, { adapter: mockAdapter })).rejects.toThrow(
await expect(postEvent({ body: input, headers: {} }, { adapter: mockAdapter })).rejects.toThrow(
new GenericError('Invalid request body', { path: '/event' })
).catch(() => { });
expect(mockAdapter.createManyEvents).not.toHaveBeenCalled();
Expand Down
4 changes: 1 addition & 3 deletions packages/core/test/pageview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { Adapter } from "../src";

describe('pageViewPost', () => {
const mockAdapter = {
tracker: {
createPageView: vi.fn(),
},
createPageView: vi.fn(),
} as unknown as Adapter;

beforeEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { describe, expect, it, vi } from "vitest";
import { Adapter } from "../src/adapters";
import { internalRouter } from "../src/router";
Expand All @@ -15,7 +16,7 @@ describe('internalRouter', () => {
adapter: vi.fn() as unknown as Adapter
};

type ValidRequest = ApiRequest<{ path: string }>
type ValidRequest = ApiRequest<{ path: string }, any>
it('should return an error if the request body is not an object', async () => {
const req = mockRequest('POST', '/test', 'invalid', {}) as unknown as ValidRequest
req.body = JSON.stringify(req.body) as unknown as { path: '/' }
Expand Down
16 changes: 10 additions & 6 deletions packages/core/test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ vi.mock("../src/router/routes/utils/detect/getIpAddress")

describe('sessionPost', () => {
const mockAdapter = {
tracker: {
createSession: vi.fn().mockResolvedValue({ message: 'Success', code: 200 }),
},
createSession: vi.fn().mockResolvedValue({ message: 'Success', code: 200 }),
} as unknown as Adapter
const mockOptions: LogLibOptions = {
adapter: mockAdapter,
Expand Down Expand Up @@ -56,12 +54,18 @@ describe('sessionPost', () => {
expect(res).toEqual({ message: 'bot', code: 200 });
});

it('should return an error if the location is invalid', async () => {
it('should throw error if there is no way to get location', async () => {
const req = mockRequest(validRequest);
const mockGetLocation = vi.fn().mockResolvedValue(null);
const res = await sessionPost(req, { ...mockOptions, getLocation: mockGetLocation });
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(sessionPost(req, { ...mockOptions, getLocation: mockGetLocation })).rejects.toThrowError();
});

it('should pass on custom location getter implementation', async () => {
const req = mockRequest(validRequest);
const mockGetLocation = vi.fn().mockResolvedValue({ city: 'Test City', country: 'Test Country' });
await sessionPost(req, { ...mockOptions, getLocation: mockGetLocation })
expect(mockGetLocation).toHaveBeenCalled();
expect(res).toEqual({ message: 'Invalid location', code: 400 });
});

it('should return a localhost message if the IP address is localhost', async () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/test/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { Adapter } from "../src";

describe('userPost', () => {
const mockAdapter = {
tracker: {
updateUser: vi.fn(),
},
updateUser: vi.fn(),
} as unknown as Adapter;

beforeEach(() => {
Expand Down

0 comments on commit 903dfe2

Please sign in to comment.