Skip to content

Commit

Permalink
[RSN-9] - Implemented DTO models on frontend (#15)
Browse files Browse the repository at this point in the history

Co-authored-by: raczu <[email protected]>
  • Loading branch information
wzarek and raczu authored Jun 9, 2024
1 parent 280aae9 commit d99aaab
Show file tree
Hide file tree
Showing 27 changed files with 2,141 additions and 16 deletions.
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: 2 additions & 1 deletion Client/reasn-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"autoprefixer": "^10.4.19",
"clsx": "^2.1.1",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3"
"tailwindcss": "^3.4.3",
"zod": "^3.23.7"
},
"lint-staged": {
"apps/**/*.{js,ts,jsx,tsx}": [
Expand Down
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,
);
});
});
});
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"));
});
});
});
Loading

0 comments on commit d99aaab

Please sign in to comment.