Skip to content

Commit

Permalink
test: implemented unit tests for models and their mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wzarek committed May 8, 2024
1 parent 5ce6e8d commit c5b772a
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 385 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressDto } from '@reasn/common/models/AddressDto'
import { AddressDtoMapper, AddressDto } from '@reasn/common/models/AddressDto'
import ModelMappingError from '@reasn/common/errors/ModelMappingError'

describe('AddressDto', () => {
Expand All @@ -8,19 +8,6 @@ describe('AddressDto', () => {
const state = 'Test State'
const zipCode = '12345'

describe('constructor', () => {
it('should create an instance of AddressDto', () => {
const address = new AddressDto(country, city, street, state, zipCode)

expect(address).toBeInstanceOf(AddressDto)
expect(address.Country).toBe(country)
expect(address.City).toBe(city)
expect(address.Street).toBe(street)
expect(address.State).toBe(state)
expect(address.ZipCode).toBe(zipCode)
})
})

describe('fromJson', () => {
it('should create an instance of AddressDto from JSON string', () => {
const json = `{
Expand All @@ -31,9 +18,7 @@ describe('AddressDto', () => {
"ZipCode": "${zipCode}"
}`

let address = AddressDto.fromJson(json)

expect(address).toBeInstanceOf(AddressDto)
let address = AddressDtoMapper.fromJSON(json)
address = address as AddressDto

expect(address.Country).toBe(country)
Expand All @@ -43,12 +28,8 @@ describe('AddressDto', () => {
expect(address.ZipCode).toBe(zipCode)
})

it('should return null if the JSON string is empty', () => {
const json = ''

const address = AddressDto.fromJson(json)

expect(address).toBeNull()
it('should throw error if the JSON string is empty', () => {
expect(() => AddressDtoMapper.fromJSON('')).toThrow(ModelMappingError)
})

it('should throw an error when providing JSON without each property individually', () => {
Expand Down Expand Up @@ -87,15 +68,11 @@ describe('AddressDto', () => {
"State": "${state}"
}`

expect(() => AddressDto.fromJson(jsonWithoutCountry)).toThrow(ModelMappingError)
expect(() => AddressDto.fromJson(jsonWithoutCity)).toThrow(ModelMappingError)
expect(() => AddressDto.fromJson(jsonWithoutStreet)).toThrow(ModelMappingError)
expect(() => AddressDto.fromJson(jsonWithoutState)).toThrow(ModelMappingError)

let addressWithoutZipcode = AddressDto.fromJson(jsonWithoutZipCode)
expect(addressWithoutZipcode).toBeInstanceOf(AddressDto)
addressWithoutZipcode = addressWithoutZipcode as AddressDto
expect(addressWithoutZipcode.ZipCode).toBeNull()
expect(() => AddressDtoMapper.fromJSON(jsonWithoutCountry)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromJSON(jsonWithoutCity)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromJSON(jsonWithoutStreet)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromJSON(jsonWithoutState)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromJSON(jsonWithoutZipCode)).toThrow(ModelMappingError)
})
})

Expand All @@ -109,9 +86,7 @@ describe('AddressDto', () => {
ZipCode: zipCode
}

let address = AddressDto.fromObject(object)

expect(address).toBeInstanceOf(AddressDto)
let address = AddressDtoMapper.fromObject(object)
address = address as AddressDto

expect(address.Country).toBe(country)
Expand Down Expand Up @@ -165,16 +140,12 @@ describe('AddressDto', () => {
State: state
}

expect(() => AddressDto.fromObject(object)).toThrow(ModelMappingError)
expect(() => AddressDto.fromObject(objectWithoutCountry)).toThrow(ModelMappingError)
expect(() => AddressDto.fromObject(objectWithoutCity)).toThrow(ModelMappingError)
expect(() => AddressDto.fromObject(objectWithoutStreet)).toThrow(ModelMappingError)
expect(() => AddressDto.fromObject(objectWithoutState)).toThrow(ModelMappingError)

let addressWithoutZipcode = AddressDto.fromObject(objectWithoutZipCode)
expect(addressWithoutZipcode).toBeInstanceOf(AddressDto)
addressWithoutZipcode = addressWithoutZipcode as AddressDto
expect(addressWithoutZipcode.ZipCode).toBeNull()
expect(() => AddressDtoMapper.fromObject(object)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromObject(objectWithoutCountry)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromObject(objectWithoutCity)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromObject(objectWithoutStreet)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromObject(objectWithoutState)).toThrow(ModelMappingError)
expect(() => AddressDtoMapper.fromObject(objectWithoutZipCode)).toThrow(ModelMappingError)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import ModelMappingError from '@reasn/common/errors/ModelMappingError'
import { CommentDto } from '@reasn/common/models/CommentDto'
import { CommentDto, CommentDtoMapper } from '@reasn/common/models/CommentDto'

describe('CommentDto', () => {
const eventId = 1
const content = 'Test Content'
const createdAt = new Date()
const userId = 2

describe('constructor', () => {
it('should create an instance of CommentDto', () => {
const comment = new CommentDto(eventId, content, createdAt, userId)

expect(comment).toBeInstanceOf(CommentDto)
expect(comment.EventId).toBe(eventId)
expect(comment.Content).toBe(content)
expect(comment.CreatedAt).toBe(createdAt)
expect(comment.UserId).toBe(userId)
})
})

describe('fromJson', () => {
it('should create an instance of CommentDto from JSON string', () => {
const json = `{
Expand All @@ -28,9 +16,7 @@ describe('CommentDto', () => {
"UserId": ${userId}
}`

let comment = CommentDto.fromJson(json)

expect(comment).toBeInstanceOf(CommentDto)
let comment = CommentDtoMapper.fromJSON(json)
comment = comment as CommentDto

expect(comment.EventId).toBe(eventId)
Expand All @@ -39,12 +25,8 @@ describe('CommentDto', () => {
expect(comment.UserId).toBe(userId)
})

it('should return null if the JSON string is empty', () => {
const json = ''

const comment = CommentDto.fromJson(json)

expect(comment).toBeNull()
it('should throw an error if the JSON string is empty', () => {
expect(() => CommentDtoMapper.fromJSON('')).toThrow(ModelMappingError)
})

it('should throw an error when providing JSON without each property individually', () => {
Expand Down Expand Up @@ -72,10 +54,10 @@ describe('CommentDto', () => {
"CreatedAt": "${createdAt.toISOString()}"
}`

expect(() => CommentDto.fromJson(jsonWithoutEventId)).toThrow(ModelMappingError)
expect(() => CommentDto.fromJson(jsonWithoutContent)).toThrow(ModelMappingError)
expect(() => CommentDto.fromJson(jsonWithoutCreatedAt)).toThrow(ModelMappingError)
expect(() => CommentDto.fromJson(jsonWithoutUserId)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromJSON(jsonWithoutEventId)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromJSON(jsonWithoutContent)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromJSON(jsonWithoutCreatedAt)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromJSON(jsonWithoutUserId)).toThrow(ModelMappingError)
})
})

Expand All @@ -88,9 +70,7 @@ describe('CommentDto', () => {
UserId: userId
}

let comment = CommentDto.fromObject(object)

expect(comment).toBeInstanceOf(CommentDto)
let comment = CommentDtoMapper.fromObject(object)
comment = comment as CommentDto

expect(comment.EventId).toBe(eventId)
Expand Down Expand Up @@ -131,11 +111,11 @@ describe('CommentDto', () => {
CreatedAt: createdAt
}

expect(() => CommentDto.fromObject(object)).toThrow(ModelMappingError)
expect(() => CommentDto.fromObject(objectWithoutEventId)).toThrow(ModelMappingError)
expect(() => CommentDto.fromObject(objectWithoutContent)).toThrow(ModelMappingError)
expect(() => CommentDto.fromObject(objectWithoutCreatedAt)).toThrow(ModelMappingError)
expect(() => CommentDto.fromObject(objectWithoutUserId)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(object)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectWithoutEventId)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectWithoutContent)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectWithoutCreatedAt)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectWithoutUserId)).toThrow(ModelMappingError)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventDto } from '@reasn/common/models/EventDto'
import { EventDto, EventDtoMapper } from '@reasn/common/models/EventDto'
import ModelMappingError from '@reasn/common/errors/ModelMappingError'
import { TagDto } from '@reasn/common/models/TagDto'

Expand All @@ -15,37 +15,6 @@ describe('EventDto', () => {
const statusId = 1
const tags: TagDto[] = [{ Name: 'Tag 1' }, { Name: 'Tag 2' }]

describe('constructor', () => {
it('should create an instance of EventDto', () => {
const event = new EventDto(
name,
addressId,
description,
organizerId,
startAt,
endAt,
createdAt,
updatedAt,
slug,
statusId,
tags
)

expect(event).toBeInstanceOf(EventDto)
expect(event.Name).toBe(name)
expect(event.AddressId).toBe(addressId)
expect(event.Description).toBe(description)
expect(event.OrganizerId).toBe(organizerId)
expect(event.StartAt).toBe(startAt)
expect(event.EndAt).toBe(endAt)
expect(event.CreatedAt).toBe(createdAt)
expect(event.UpdatedAt).toBe(updatedAt)
expect(event.Slug).toBe(slug)
expect(event.StatusId).toBe(statusId)
expect(event.Tags).toBe(tags)
})
})

describe('fromJson', () => {
it('should create an instance of EventDto from JSON string', () => {
const json = `{
Expand All @@ -62,9 +31,7 @@ describe('EventDto', () => {
"Tags": ${JSON.stringify(tags)}
}`

let event = EventDto.fromJson(json)

expect(event).toBeInstanceOf(EventDto)
let event = EventDtoMapper.fromJSON(json)
event = event as EventDto

expect(event.Name).toBe(name)
Expand All @@ -81,11 +48,7 @@ describe('EventDto', () => {
})

it('should return null if the JSON string is empty', () => {
const json = ''

const event = EventDto.fromJson(json)

expect(event).toBeNull()
expect(() => EventDtoMapper.fromJSON('')).toThrow(ModelMappingError)
})

it('should throw an error when providing json without each property individually', () => {
Expand Down Expand Up @@ -233,37 +196,37 @@ describe('EventDto', () => {
}`

expect(() => {
EventDto.fromJson(jsonWithoutName)
EventDtoMapper.fromJSON(jsonWithoutName)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutAddressId)
EventDtoMapper.fromJSON(jsonWithoutAddressId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutDescription)
EventDtoMapper.fromJSON(jsonWithoutDescription)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutOrganizerId)
EventDtoMapper.fromJSON(jsonWithoutOrganizerId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutStartAt)
EventDtoMapper.fromJSON(jsonWithoutStartAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutEndAt)
EventDtoMapper.fromJSON(jsonWithoutEndAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutCreatedAt)
EventDtoMapper.fromJSON(jsonWithoutCreatedAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutUpdatedAt)
EventDtoMapper.fromJSON(jsonWithoutUpdatedAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutSlug)
EventDtoMapper.fromJSON(jsonWithoutSlug)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutStatusId)
EventDtoMapper.fromJSON(jsonWithoutStatusId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromJson(jsonWithoutTags)
EventDtoMapper.fromJSON(jsonWithoutTags)
}).toThrow(ModelMappingError)
})
})
Expand All @@ -284,9 +247,7 @@ describe('EventDto', () => {
Tags: tags
}

let event = EventDto.fromObject(obj)

expect(event).toBeInstanceOf(EventDto)
let event = EventDtoMapper.fromObject(obj)
event = event as EventDto

expect(event.Name).toBe(name)
Expand Down Expand Up @@ -461,40 +422,40 @@ describe('EventDto', () => {
}

expect(() => {
EventDto.fromObject(invalidObj)
EventDtoMapper.fromObject(invalidObj)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutName)
EventDtoMapper.fromObject(objWithoutName)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutAddressId)
EventDtoMapper.fromObject(objWithoutAddressId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutDescription)
EventDtoMapper.fromObject(objWithoutDescription)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutOrganizerId)
EventDtoMapper.fromObject(objWithoutOrganizerId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutStartAt)
EventDtoMapper.fromObject(objWithoutStartAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutEndAt)
EventDtoMapper.fromObject(objWithoutEndAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutCreatedAt)
EventDtoMapper.fromObject(objWithoutCreatedAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutUpdatedAt)
EventDtoMapper.fromObject(objWithoutUpdatedAt)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutSlug)
EventDtoMapper.fromObject(objWithoutSlug)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutStatusId)
EventDtoMapper.fromObject(objWithoutStatusId)
}).toThrow(ModelMappingError)
expect(() => {
EventDto.fromObject(objWithoutTags)
EventDtoMapper.fromObject(objWithoutTags)
}).toThrow(ModelMappingError)
})
})
Expand Down
Loading

0 comments on commit c5b772a

Please sign in to comment.