valid and invalid URL of the url-shortener.service.spec.ts

This commit is contained in:
Kfir Dayan 2024-01-21 11:49:45 +02:00
parent 5e5a584042
commit 23bdef3fbc
4 changed files with 24 additions and 41 deletions

View file

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View file

@ -7,6 +7,7 @@ export class DnsService {
private lookup = util.promisify(dns.lookup); private lookup = util.promisify(dns.lookup);
async isValidUrl(url: string): Promise<boolean> { async isValidUrl(url: string): Promise<boolean> {
console.log(url)
try { try {
const hostname = new URL(url).hostname; const hostname = new URL(url).hostname;
await this.lookup(hostname); await this.lookup(hostname);

View file

@ -1,18 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UrlShortenerController } from './url-shortener.controller';
describe('UrlShortenerController', () => {
let controller: UrlShortenerController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UrlShortenerController],
}).compile();
controller = module.get<UrlShortenerController>(UrlShortenerController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View file

@ -1,18 +1,40 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { UrlShortenerService } from './url-shortener.service'; import { UrlShortenerService } from './url-shortener.service';
import { DnsService } from '../dns/dns.service';
describe('UrlShortenerService', () => { describe('UrlShortenerService', () => {
let service: UrlShortenerService; let service: UrlShortenerService;
let dnsService: DnsService;
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [UrlShortenerService], providers: [
UrlShortenerService,
{ provide: DnsService, useValue: { isValidUrl: jest.fn().mockResolvedValue(true) } },
],
}).compile(); }).compile();
service = module.get<UrlShortenerService>(UrlShortenerService); service = module.get<UrlShortenerService>(UrlShortenerService);
dnsService = module.get<DnsService>(DnsService);
}); });
it('should be defined', () => { it('should be defined', () => {
expect(service).toBeDefined(); expect(service).toBeDefined();
}); });
it('should generate and retrieve a short URL', async () => {
jest.spyOn(dnsService, 'isValidUrl').mockResolvedValue(true);
const url = 'https://example.com';
const shortUrl = await service.generateShortUrl(url);
expect(shortUrl).toBeDefined();
expect(await service.getOriginalUrl(shortUrl)).toBe(url);
});
it('should throw an error for an invalid URL', async () => {
jest.spyOn(dnsService, 'isValidUrl').mockResolvedValue(false);
const url = 'http://invalid-url';
await expect(service.generateShortUrl(url)).rejects.toThrow('URL is invalid');
});
}); });