Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSN-9] - Implemented DTO models on frontend #15

Merged
merged 25 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1aaaa5c
Implemented DTO models on frontend with mappers from object/JSON stri…
wzarek Apr 22, 2024
64b053e
feat: changed DTOs to use zod
wzarek May 7, 2024
8e72897
fix: minor fixes for error message and handling empty json
wzarek May 8, 2024
2e664a8
test: implemented unit tests for models and their mappers
wzarek May 8, 2024
b334159
fix: uploaded old yarn.lock file
wzarek May 8, 2024
728469e
fix: update unit tests workflow for frontend
wzarek May 8, 2024
70f3014
fix: added newer version of zod
wzarek May 8, 2024
c785603
fix: update unit tests workflow to not use cache
wzarek May 8, 2024
b8be3e8
fix: added handling for date format
wzarek May 10, 2024
0c56741
Implemented DTO models on frontend with mappers from object/JSON stri…
wzarek Apr 22, 2024
3415415
feat: changed DTOs to use zod
wzarek May 7, 2024
22a24e3
fix: minor fixes for error message and handling empty json
wzarek May 8, 2024
b60ca79
test: implemented unit tests for models and their mappers
wzarek May 8, 2024
3759146
fix: uploaded old yarn.lock file
wzarek May 8, 2024
baa1dae
fix: update unit tests workflow for frontend
wzarek May 8, 2024
6e469b5
fix: added newer version of zod
wzarek May 8, 2024
e37b728
fix: update unit tests workflow to not use cache
wzarek May 8, 2024
a4ac370
fix: added handling for date format
wzarek May 10, 2024
3330bf8
fix: added deps
wzarek May 10, 2024
23d2466
Merge branch 'RSN-9' of https://github.com/wzarek/Reasn into RSN-9
wzarek May 10, 2024
4222524
fix: expanded our custom exception to get ZodIssues
wzarek May 13, 2024
66acb32
fix: fixed error handling for zod
wzarek May 15, 2024
489af11
refactor: adapt dtos after changes on the backend
raczu Jun 8, 2024
520be86
Merge remote-tracking branch 'origin/main' into RSN-9
raczu Jun 8, 2024
23ca881
chore: add missing files
raczu Jun 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
run: yarn set version 4.1

- name: Install dependencies
run: yarn install --frozen-lockfile

run: yarn install --immutable
- name: Test with jest
run: yarn test --coverage --ci
3 changes: 3 additions & 0 deletions Client/reasn-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
},
"resolutions": {
"uuid": "^3.4.0"
},
"dependencies": {
"zod": "^3.23.7"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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)
})
})
})
Loading
Loading