Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuyacatcat committed Mar 24, 2024
1 parent 74cae68 commit 6ae07bb
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 64 deletions.
1 change: 0 additions & 1 deletion app/service/stampService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// stampService.js
import { StampRepository } from '../repository/stampRepository';

export class StampService {
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
testMatch: ["**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"],
preset: 'ts-jest',
testMatch: ['**/tests/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dev:monorepo": "next dev",
"build": "next build",
"start": "next start",
"clean": "rimraf node_modules",
"format": "prettier --write 'app/**/*.{ts,tsx,json,md}'",
"format:fix": "prettier --write 'app/**/*.{ts,tsx,json,md}'",
"format:check": "prettier --check 'app/**/*.{ts,tsx,js,jsx,json,css}'",
Expand Down Expand Up @@ -43,6 +44,7 @@
},
"devDependencies": {
"@frames.js/debugger": "^0.1.9",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.30",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
Expand Down
6 changes: 0 additions & 6 deletions tests/sample.test.ts

This file was deleted.

41 changes: 41 additions & 0 deletions tests/stampRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// StampRepository.test.ts
import OpenAI from 'openai';
import { StampRepository } from '../app/repository/stampRepository';

describe('StampRepository', () => {
let openAIInstance: OpenAI;
let stampRepository: StampRepository;
const mockImageUrl = 'https://example.com/image.png';

beforeEach(() => {
openAIInstance = new OpenAI({ apiKey: 'dummy-api-key' });
stampRepository = new StampRepository(openAIInstance);
});

it('should generate an image URL successfully', async () => {
jest.spyOn(openAIInstance.images, 'generate').mockResolvedValue({
created: 0,
data: [{ url: mockImageUrl }],
});

const prompt = 'test prompt';
const imageUrl = await stampRepository.generateImage(prompt);
expect(imageUrl).toBe(mockImageUrl);
expect(openAIInstance.images.generate).toHaveBeenCalledWith({
model: 'dall-e-3',
prompt,
n: 1,
size: '1024x1024',
});
});

it('should throw an error when image generation fails', async () => {
jest
.spyOn(openAIInstance.images, 'generate')
.mockRejectedValue(new Error('API call failed'));

await expect(stampRepository.generateImage('test prompt')).rejects.toThrow(
'Image generation failed'
);
});
});
54 changes: 0 additions & 54 deletions tests/stampRepository.test.ts_tmp

This file was deleted.

31 changes: 31 additions & 0 deletions tests/stampService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// stampService.test.js
import { StampRepository } from '../app/repository/stampRepository';
import { StampService } from '../app/service/stampService';

describe('StampService', () => {
let service: StampService;
const template = 'Template: ';
const mockImageUrl = 'https://example.com/image.png';

beforeEach(() => {
jest.clearAllMocks();
service = new StampService(template, StampRepository.prototype);

jest
.spyOn(StampRepository.prototype, 'generateImage')
.mockImplementation((prompt) => {
return Promise.resolve(mockImageUrl);
});
});

it('should generate an image URL using the provided template and input', async () => {
const input = 'test input';
const result = await service.generateImageUrl(input);

const expectedPrompt = template + input;
expect(StampRepository.prototype.generateImage).toHaveBeenCalledWith(
expectedPrompt
);
expect(result).toBe(mockImageUrl);
});
});
Empty file removed tests/stampService.test.ts_tmp
Empty file.
12 changes: 10 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,14 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^29.5.12":
version "29.5.12"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"
integrity sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
Expand Down Expand Up @@ -4726,7 +4734,7 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==

expect@^29.7.0:
expect@^29.0.0, expect@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/expect/-/expect-29.7.0.tgz#578874590dcb3214514084c08115d8aee61e11bc"
integrity sha512-2Zks0hf1VLFYI1kbh0I5jP3KHHyCHpkfyHBzsSXRFgl/Bg9mWYfMW8oD+PdMPlEwy5HNsR9JutYy6pMeOh61nw==
Expand Down Expand Up @@ -7713,7 +7721,7 @@ prettier@^3.2.5:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368"
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==

pretty-format@^29.7.0:
pretty-format@^29.0.0, pretty-format@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812"
integrity sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==
Expand Down

0 comments on commit 6ae07bb

Please sign in to comment.