-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSN-9] - Implemented DTO models on frontend (#15)
Co-authored-by: raczu <[email protected]>
- Loading branch information
Showing
27 changed files
with
2,141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
Client/reasn-client/packages/common/__tests__/models/AddressDto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { AddressDtoMapper, AddressDto } from "@reasn/common/models/AddressDto"; | ||
import ModelMappingError from "@reasn/common/errors/ModelMappingError"; | ||
|
||
describe("AddressDto", () => { | ||
const country = "Test Country"; | ||
const city = "Test City"; | ||
const street = "Test Street"; | ||
const state = "Test State"; | ||
const zipCode = "12345"; | ||
|
||
describe("fromJson", () => { | ||
it("should create an instance of AddressDto from JSON string", () => { | ||
const json = `{ | ||
"Country": "${country}", | ||
"City": "${city}", | ||
"Street": "${street}", | ||
"State": "${state}", | ||
"ZipCode": "${zipCode}" | ||
}`; | ||
|
||
let address = AddressDtoMapper.fromJSON(json); | ||
address = address as 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); | ||
}); | ||
|
||
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", () => { | ||
const jsonWithoutCountry = `{ | ||
"City": "${city}", | ||
"Street": "${street}", | ||
"State": "${state}", | ||
"ZipCode": "${zipCode}" | ||
}`; | ||
|
||
const jsonWithoutCity = `{ | ||
"Country": "${country}", | ||
"Street": "${street}", | ||
"State": "${state}", | ||
"ZipCode": "${zipCode}" | ||
}`; | ||
|
||
const jsonWithoutStreet = `{ | ||
"Country": "${country}", | ||
"City": "${city}", | ||
"State": "${state}", | ||
"ZipCode": "${zipCode}" | ||
}`; | ||
|
||
const jsonWithoutState = `{ | ||
"Country": "${country}", | ||
"City": "${city}", | ||
"Street": "${street}", | ||
"ZipCode": "${zipCode}" | ||
}`; | ||
|
||
const jsonWithoutZipCode = `{ | ||
"Country": "${country}", | ||
"City": "${city}", | ||
"Street": "${street}", | ||
"State": "${state}" | ||
}`; | ||
|
||
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, | ||
); | ||
}); | ||
}); | ||
|
||
describe("fromObject", () => { | ||
it("should create an instance of AddressDto from an object", () => { | ||
const object = { | ||
Country: country, | ||
City: city, | ||
Street: street, | ||
State: state, | ||
ZipCode: zipCode, | ||
}; | ||
|
||
let address = AddressDtoMapper.fromObject(object); | ||
address = address as 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); | ||
}); | ||
|
||
it("should throw an error if the object is invalid", () => { | ||
const object = { | ||
Country: country, | ||
City: true, | ||
Street: street, | ||
State: state, | ||
ZipCode: null, | ||
}; | ||
|
||
const objectWithoutCountry = { | ||
City: city, | ||
Street: street, | ||
State: state, | ||
ZipCode: zipCode, | ||
}; | ||
|
||
const objectWithoutCity = { | ||
Country: country, | ||
Street: street, | ||
State: state, | ||
ZipCode: zipCode, | ||
}; | ||
|
||
const objectWithoutStreet = { | ||
Country: country, | ||
City: city, | ||
State: state, | ||
ZipCode: zipCode, | ||
}; | ||
|
||
const objectWithoutState = { | ||
Country: country, | ||
City: city, | ||
Street: street, | ||
ZipCode: zipCode, | ||
}; | ||
|
||
const objectWithoutZipCode = { | ||
Country: country, | ||
City: city, | ||
Street: street, | ||
State: state, | ||
}; | ||
|
||
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, | ||
); | ||
}); | ||
}); | ||
}); |
204 changes: 204 additions & 0 deletions
204
Client/reasn-client/packages/common/__tests__/models/CommentDto.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
import ModelMappingError from "@reasn/common/errors/ModelMappingError"; | ||
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("fromJson", () => { | ||
it("should create an instance of CommentDto from JSON string", () => { | ||
const json = `{ | ||
"EventId": ${eventId}, | ||
"Content": "${content}", | ||
"CreatedAt": "${createdAt.toISOString()}", | ||
"UserId": ${userId} | ||
}`; | ||
|
||
let comment = CommentDtoMapper.fromJSON(json); | ||
comment = comment as CommentDto; | ||
|
||
expect(comment.EventId).toBe(eventId); | ||
expect(comment.Content).toBe(content); | ||
expect(comment.CreatedAt).toEqual(createdAt); | ||
expect(comment.UserId).toBe(userId); | ||
}); | ||
|
||
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", () => { | ||
const jsonWithoutEventId = `{ | ||
"Content": "${content}", | ||
"CreatedAt": "${createdAt.toISOString()}", | ||
"UserId": ${userId} | ||
}`; | ||
|
||
const jsonWithoutContent = `{ | ||
"EventId": ${eventId}, | ||
"CreatedAt": "${createdAt.toISOString()}", | ||
"UserId": ${userId} | ||
}`; | ||
|
||
const jsonWithoutCreatedAt = `{ | ||
"EventId": ${eventId}, | ||
"Content": "${content}", | ||
"UserId": ${userId} | ||
}`; | ||
|
||
const jsonWithoutUserId = `{ | ||
"EventId": ${eventId}, | ||
"Content": "${content}", | ||
"CreatedAt": "${createdAt.toISOString()}" | ||
}`; | ||
|
||
expect(() => CommentDtoMapper.fromJSON(jsonWithoutEventId)).toThrow( | ||
ModelMappingError, | ||
); | ||
expect(() => CommentDtoMapper.fromJSON(jsonWithoutContent)).toThrow( | ||
ModelMappingError, | ||
); | ||
expect(() => CommentDtoMapper.fromJSON(jsonWithoutCreatedAt)).toThrow( | ||
ModelMappingError, | ||
); | ||
expect(() => CommentDtoMapper.fromJSON(jsonWithoutUserId)).toThrow( | ||
ModelMappingError, | ||
); | ||
}); | ||
}); | ||
|
||
describe("fromObject", () => { | ||
it("should create an instance of CommentDto from an object", () => { | ||
const object = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: createdAt, | ||
UserId: userId, | ||
}; | ||
|
||
let comment = CommentDtoMapper.fromObject(object); | ||
comment = comment as CommentDto; | ||
|
||
expect(comment.EventId).toBe(eventId); | ||
expect(comment.Content).toBe(content); | ||
expect(comment.CreatedAt).toEqual(createdAt); | ||
expect(comment.UserId).toBe(userId); | ||
}); | ||
|
||
it("should throw an error if the object is invalid", () => { | ||
const object = { | ||
EventId: eventId, | ||
Content: true, | ||
CreatedAt: createdAt, | ||
UserId: null, | ||
}; | ||
|
||
const objectWithoutEventId = { | ||
Content: content, | ||
CreatedAt: createdAt, | ||
UserId: userId, | ||
}; | ||
|
||
const objectWithoutContent = { | ||
EventId: eventId, | ||
CreatedAt: createdAt, | ||
UserId: userId, | ||
}; | ||
|
||
const objectWithoutCreatedAt = { | ||
EventId: eventId, | ||
Content: content, | ||
UserId: userId, | ||
}; | ||
|
||
const objectWithoutUserId = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: createdAt, | ||
}; | ||
|
||
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, | ||
); | ||
}); | ||
|
||
it("should throw an error if date is in incorrect format", () => { | ||
const object = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: "invalid date", | ||
UserId: userId, | ||
}; | ||
|
||
const objectMissingZ = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: "2009-06-15T13:45:30.0000000", | ||
UserId: userId, | ||
}; | ||
|
||
expect(() => CommentDtoMapper.fromObject(object)).toThrow( | ||
ModelMappingError, | ||
); | ||
expect(() => CommentDtoMapper.fromObject(objectMissingZ)).toThrow( | ||
ModelMappingError, | ||
); | ||
}); | ||
|
||
it("should properly parse date string", () => { | ||
const object = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: "2009-06-15T13:45:30.0000000-07:00", | ||
UserId: userId, | ||
}; | ||
|
||
const objectWithoutOffset = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: "2009-06-15T13:45:30.0000000Z", | ||
UserId: userId, | ||
}; | ||
|
||
const objectWithoutMilliseconds = { | ||
EventId: eventId, | ||
Content: content, | ||
CreatedAt: "2009-06-15T13:45:30Z", | ||
UserId: userId, | ||
}; | ||
|
||
let comment = CommentDtoMapper.fromObject(object); | ||
comment = comment as CommentDto; | ||
|
||
expect(comment.CreatedAt).toEqual( | ||
new Date("2009-06-15T13:45:30.0000000-07:00"), | ||
); | ||
|
||
comment = CommentDtoMapper.fromObject(objectWithoutOffset); | ||
comment = comment as CommentDto; | ||
|
||
expect(comment.CreatedAt).toEqual( | ||
new Date("2009-06-15T13:45:30.0000000Z"), | ||
); | ||
|
||
comment = CommentDtoMapper.fromObject(objectWithoutMilliseconds); | ||
comment = comment as CommentDto; | ||
|
||
expect(comment.CreatedAt).toEqual(new Date("2009-06-15T13:45:30Z")); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.