Skip to content

Commit

Permalink
Merge pull request #26 from w3bdesign/backend
Browse files Browse the repository at this point in the history
More tests
  • Loading branch information
w3bdesign authored Nov 19, 2024
2 parents b41093e + 38ad842 commit 677b1ae
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 4 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,31 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: './backend/package.json'

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: npm ci
run: pnpm install

- name: Run tests
run: npm test -- --coverage
run: pnpm test -- --coverage

- name: Upload results to Codecov
uses: codecov/codecov-action@v4
Expand Down
115 changes: 115 additions & 0 deletions backend/src/services/services.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ServicesService } from './services.service';
import { Service } from './entities/service.entity';
import { NotFoundException } from '@nestjs/common';

describe('ServicesService', () => {
let service: ServicesService;
let serviceRepository: Repository<Service>;

const mockService = {
id: 'service-1',
name: 'Haircut',
description: 'Basic haircut service',
duration: 60,
price: 50,
};

const mockServiceRepository = {
findOne: jest.fn(),
find: jest.fn(),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ServicesService,
{
provide: getRepositoryToken(Service),
useValue: mockServiceRepository,
},
],
}).compile();

service = module.get<ServicesService>(ServicesService);
serviceRepository = module.get<Repository<Service>>(getRepositoryToken(Service));
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findOne', () => {
it('should return a service when found', async () => {
mockServiceRepository.findOne.mockResolvedValue(mockService);

const result = await service.findOne('service-1');
expect(result).toEqual(mockService);
expect(serviceRepository.findOne).toHaveBeenCalledWith({
where: { id: 'service-1' },
});
});

it('should throw NotFoundException when service not found', async () => {
mockServiceRepository.findOne.mockResolvedValue(null);

await expect(service.findOne('non-existent')).rejects.toThrow(
NotFoundException,
);
});
});

describe('findAll', () => {
it('should return all services', async () => {
const mockServices = [mockService];
mockServiceRepository.find.mockResolvedValue(mockServices);

const result = await service.findAll();
expect(result).toEqual(mockServices);
expect(serviceRepository.find).toHaveBeenCalled();
});

it('should return empty array when no services exist', async () => {
mockServiceRepository.find.mockResolvedValue([]);

const result = await service.findAll();
expect(result).toEqual([]);
expect(serviceRepository.find).toHaveBeenCalled();
});
});

describe('findByEmployee', () => {
it('should return services for an employee', async () => {
const mockServices = [mockService];
mockServiceRepository.find.mockResolvedValue(mockServices);

const result = await service.findByEmployee('employee-1');
expect(result).toEqual(mockServices);
expect(serviceRepository.find).toHaveBeenCalledWith({
where: {
employees: {
id: 'employee-1',
},
},
relations: ['employees'],
});
});

it('should return empty array when employee has no services', async () => {
mockServiceRepository.find.mockResolvedValue([]);

const result = await service.findByEmployee('employee-1');
expect(result).toEqual([]);
expect(serviceRepository.find).toHaveBeenCalledWith({
where: {
employees: {
id: 'employee-1',
},
},
relations: ['employees'],
});
});
});
});

0 comments on commit 677b1ae

Please sign in to comment.