timezone.controller.spec.tsβ’4.11 kB
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { BadRequestException } from '@nestjs/common'
import { TimezoneController } from './timezone.controller'
import { TimezoneService } from './timezone.service'
describe('TimezoneController', () => {
let controller: TimezoneController
let service: TimezoneService
beforeEach(() => {
service = new TimezoneService()
controller = new TimezoneController(service)
})
describe('getRegions', () => {
it('should return list of regions with count', () => {
const result = controller.getRegions()
expect(result).toHaveProperty('regions')
expect(result).toHaveProperty('count')
expect(Array.isArray(result.regions)).toBe(true)
expect(result.count).toBe(result.regions.length)
expect(result.count).toBeGreaterThan(0)
})
it('should call service.getRegions', () => {
const spy = vi.spyOn(service, 'getRegions')
controller.getRegions()
expect(spy).toHaveBeenCalled()
})
})
describe('getCitiesInRegion', () => {
it('should return cities for valid region', () => {
const result = controller.getCitiesInRegion('America')
expect(result).toHaveProperty('region', 'America')
expect(result).toHaveProperty('cities')
expect(result).toHaveProperty('count')
expect(Array.isArray(result.cities)).toBe(true)
expect(result.count).toBe(result.cities.length)
expect(result.count).toBeGreaterThan(0)
})
it('should throw BadRequestException for invalid region', () => {
expect(() => {
controller.getCitiesInRegion('InvalidRegion')
}).toThrow(BadRequestException)
})
it('should call service.getCitiesInRegion', () => {
const spy = vi.spyOn(service, 'getCitiesInRegion')
controller.getCitiesInRegion('Europe')
expect(spy).toHaveBeenCalledWith('Europe')
})
it('should handle service errors and throw BadRequestException', () => {
vi.spyOn(service, 'getCitiesInRegion').mockImplementation(() => {
throw new Error('Service error')
})
expect(() => {
controller.getCitiesInRegion('Test')
}).toThrow(BadRequestException)
})
})
describe('getTimeInTimezone', () => {
it('should return time for valid timezone (America/New_York)', () => {
const result = controller.getTimeInTimezone('America', 'New_York')
expect(result).toHaveProperty('timezone', 'America/New_York')
expect(result).toHaveProperty('datetime_local')
expect(result).toHaveProperty('datetime_utc')
expect(result).toHaveProperty('timezone_offset')
expect(result).toHaveProperty('timestamp')
expect(typeof result.datetime_local).toBe('string')
expect(typeof result.datetime_utc).toBe('string')
expect(typeof result.timezone_offset).toBe('string')
expect(typeof result.timestamp).toBe('number')
})
it('should return time for valid timezone (Europe/London)', () => {
const result = controller.getTimeInTimezone('Europe', 'London')
expect(result).toHaveProperty('timezone', 'Europe/London')
expect(result).toHaveProperty('datetime_local')
expect(result).toHaveProperty('datetime_utc')
expect(result).toHaveProperty('timezone_offset')
expect(result).toHaveProperty('timestamp')
})
it('should throw BadRequestException for invalid timezone', () => {
expect(() => {
controller.getTimeInTimezone('Invalid', 'Timezone')
}).toThrow(BadRequestException)
})
it('should call service.getTimeInTimezone with combined region/city', () => {
const spy = vi.spyOn(service, 'getTimeInTimezone')
controller.getTimeInTimezone('Asia', 'Tokyo')
expect(spy).toHaveBeenCalledWith('Asia/Tokyo')
})
it('should handle service errors and throw BadRequestException', () => {
vi.spyOn(service, 'getTimeInTimezone').mockImplementation(() => {
throw new Error('Service error')
})
expect(() => {
controller.getTimeInTimezone('Test', 'Error')
}).toThrow(BadRequestException)
})
})
})