|
| 1 | +import { ShortenUrlController } from './shorten-url.controller'; |
| 2 | +import { GetOriginalUrlController } from './get-original-url.controller'; |
| 3 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 4 | +import { ConfigModule } from '@nestjs/config'; |
| 5 | +import { ShortenUrlModule } from './shorten-url.module'; |
| 6 | + |
| 7 | +describe('ShortenUrl controller', () => { |
| 8 | + let shortenUrlController: ShortenUrlController; |
| 9 | + let underTest: GetOriginalUrlController; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + const app: TestingModule = await Test.createTestingModule({ |
| 13 | + imports: [await ConfigModule.forRoot(), ShortenUrlModule], |
| 14 | + }).compile(); |
| 15 | + |
| 16 | + shortenUrlController = app.get<ShortenUrlController>(ShortenUrlController); |
| 17 | + underTest = app.get<GetOriginalUrlController>(GetOriginalUrlController); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('getOriginalUrl', () => { |
| 21 | + it('should return the original URL for a given shortened URL', async () => { |
| 22 | + const longUrl = |
| 23 | + 'https://zapper.xyz/very-long-url/very-long-url/very-long-url'; |
| 24 | + |
| 25 | + // Use the shorten URL controller to create the shortened URL |
| 26 | + const shortenedResponse = await shortenUrlController.shortenUrl({ |
| 27 | + url: longUrl, |
| 28 | + }); |
| 29 | + const shortenedUrl = shortenedResponse.shortenedUrl; |
| 30 | + |
| 31 | + // Now retrieve the original URL using GetOriginalUrlController |
| 32 | + const response = await underTest.getOriginalUrl({ |
| 33 | + shortenedUrl: shortenedUrl, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(response).not.toBeNull(); |
| 37 | + expect(response.originalUrl).toBe(longUrl); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
0 commit comments