diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -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); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/dns/dns.service.ts b/src/dns/dns.service.ts index 44f26b3..f7cb00d 100644 --- a/src/dns/dns.service.ts +++ b/src/dns/dns.service.ts @@ -7,6 +7,7 @@ export class DnsService { private lookup = util.promisify(dns.lookup); async isValidUrl(url: string): Promise { + console.log(url) try { const hostname = new URL(url).hostname; await this.lookup(hostname); diff --git a/src/url-shortener/url-shortener.controller.spec.ts b/src/url-shortener/url-shortener.controller.spec.ts deleted file mode 100644 index ec2da4d..0000000 --- a/src/url-shortener/url-shortener.controller.spec.ts +++ /dev/null @@ -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); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/url-shortener/url-shortener.service.spec.ts b/src/url-shortener/url-shortener.service.spec.ts index 773741e..3f9b4fd 100644 --- a/src/url-shortener/url-shortener.service.spec.ts +++ b/src/url-shortener/url-shortener.service.spec.ts @@ -1,18 +1,40 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UrlShortenerService } from './url-shortener.service'; +import { DnsService } from '../dns/dns.service'; describe('UrlShortenerService', () => { let service: UrlShortenerService; + let dnsService: DnsService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [UrlShortenerService], + providers: [ + UrlShortenerService, + { provide: DnsService, useValue: { isValidUrl: jest.fn().mockResolvedValue(true) } }, + ], }).compile(); service = module.get(UrlShortenerService); + dnsService = module.get(DnsService); }); it('should be defined', () => { 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'); + }); + });