Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD Tests for Template Routes - [2] #903

Merged
merged 3 commits into from
May 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion server/src/controllers/template/addTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function addTemplateRoute() {
.mutation(async (opts) => {
const { type, templateId, isGlobalTemplate, createdBy } = opts.input;
const userClass = new User();
const user = await userClass.findById(createdBy);
const user = await userClass.findUser({ userId: createdBy });
if (!user) {
throw new Error(UserNotFoundError.message);
}
Expand Down
18 changes: 6 additions & 12 deletions server/src/drizzle/methods/template.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { eq } from 'drizzle-orm';
import { createDb } from '../../db/client';
import { DbClient } from '../../db/client';
import {
type InsertTemplate,
template as TemplateTable,
} from '../../db/schema';
import { getDB } from '../../trpc/context';

export class Template {
async createInstance() {
const dbInstance = await createDb(getDB());
return dbInstance;
}

async findTemplate(templateId: string, includeRelated: boolean = false) {
try {
const template = (await this.createInstance()).query.template.findFirst({
const template = await DbClient.instance.query.template.findFirst({
where: eq(TemplateTable.id, templateId),
// If includeRelated is true, then include with property
...(includeRelated && {
Expand All @@ -36,7 +30,7 @@ export class Template {

async findMany() {
try {
const templates = (await this.createInstance()).query.template.findMany({
const templates = await DbClient.instance.query.template.findMany({
with: {
createdBy: {
columns: {
Expand All @@ -53,7 +47,7 @@ export class Template {

async create(data: InsertTemplate) {
try {
const createdTemplate = (await this.createInstance())
const createdTemplate = await DbClient.instance
.insert(TemplateTable)
.values(data)
.returning()
Expand All @@ -69,7 +63,7 @@ export class Template {
filter = eq(TemplateTable.id, data.templateId),
) {
try {
const updatedTemplate = (await this.createInstance())
const updatedTemplate = await DbClient.instance
.update(TemplateTable)
.set({
type: data.type,
Expand All @@ -85,7 +79,7 @@ export class Template {

async delete(id: string, filter = eq(TemplateTable.id, id)) {
try {
const deletedTemplate = (await this.createInstance())
const deletedTemplate = await DbClient.instance
.delete(TemplateTable)
.where(filter)
.returning();
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/template/addTemplateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const addTemplateService = async (
try {
const userClass = new User();
const templateClass = new Template();
const user = await userClass.findById(createdBy);
const user = await userClass.findUser({ userId: createdBy });
if (!user) {
throw new Error('User not found!');
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/template/editTemplateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const editTemplateService = async (
) => {
try {
const templateClass = new Template();
const template = await templateClass.findUnique(templateId);
const template = await templateClass.findTemplate(templateId);
if (!template) {
throw new Error('Template not found');
}
Expand Down
82 changes: 82 additions & 0 deletions server/src/tests/routes/template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { setupTest } from '../testHelpers';
import type { trpcCaller } from '../testHelpers';
import { env } from 'cloudflare:test';
import { Template as TemplateClass } from '../../drizzle/methods/template';
import { User as UserClass } from '../../drizzle/methods/User';
import type { Template } from '../../db/schema';

describe('Template Routes', () => {
let caller: trpcCaller;
const templateClass = new TemplateClass();
const userClass = new UserClass();

let createdTemplate: Template;

beforeAll(async () => {
caller = await setupTest(env);
});

beforeEach(async () => {
createdTemplate = await templateClass.create({
templateId: 'test',
isGlobalTemplate: true,
type: 'pack',
});
});

describe('addTemplate', () => {
it('should create a template', async () => {
const user = await userClass.create({
email: '[email protected]',
name: 'test',
username: 'test',
password: 'test123',
});
const result = await caller.addTemplate({
type: 'pack',
createdBy: user.id,
isGlobalTemplate: true,
templateId: 'test-id',
});
expect(result.message).toEqual('Template added successfully');
});
});

describe('getTemplates', () => {
it('should get templates', async () => {
const templates = await caller.getTemplates();
expect(templates).toBeDefined();
});
});

describe('getTemplateById', () => {
it('should get template by id', async () => {
const foundTemplate = await caller.getTemplateById({
templateId: createdTemplate.id,
});
expect(foundTemplate.id).toEqual(createdTemplate.id);
});
});

describe('editTemplate', () => {
it('should edit template type', async () => {
const typeToBeUpdated = 'item';
const [updatedTemplate] = await caller.editTemplate({
...createdTemplate,
templateId: createdTemplate.id,
type: typeToBeUpdated,
});
expect(updatedTemplate.type).toEqual(typeToBeUpdated);
});
});

describe('deleteTemplate', () => {
it('should delete template by id', async () => {
const response = await caller.deleteTemplate({
templateId: createdTemplate.id,
});
expect(response.message).toEqual('Template removed');
});
});
});
Loading