Skip to content

Commit 32cebaf

Browse files
committed
test: add more test scenarios
1 parent 984ca21 commit 32cebaf

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

tests/Primitives.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { expectTypeOf } from "expect-type";
22

3-
import { Primitives } from "../src/Primitives";
3+
import { Primitives } from "../src";
44
import { Course } from "./Course";
55
import { DeliveryInfo } from "./DeliveryInfo";
66
import { Learner } from "./Learner";
7+
import { Product } from "./Product";
78
import { User } from "./User";
9+
import { Video } from "./Video";
810

911
describe("Primitives", () => {
1012
it("should ensure to only return primitive properties excluding methods", () => {
@@ -52,4 +54,27 @@ describe("Primitives", () => {
5254

5355
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
5456
});
57+
58+
it("should get primitive type in case it is not a value object", () => {
59+
type actualPrimitives = Primitives<Product>;
60+
61+
type expectedPrimitives = {
62+
readonly active: boolean;
63+
readonly createdAt: Date;
64+
};
65+
66+
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
67+
});
68+
69+
it("should infer the optional properties", () => {
70+
type actualPrimitives = Primitives<Video>;
71+
72+
type expectedPrimitives = {
73+
readonly id: string;
74+
readonly name: string | undefined;
75+
readonly duration: number | undefined;
76+
};
77+
78+
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
79+
});
5580
});

tests/Product.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Primitives } from "../src";
2+
3+
export class Product {
4+
constructor(
5+
public readonly active: boolean,
6+
public readonly createdAt: Date
7+
) {}
8+
9+
toPrimitives(): Primitives<Product> {
10+
return {
11+
active: this.active,
12+
createdAt: this.createdAt,
13+
};
14+
}
15+
}

tests/Video.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Primitives } from "../src";
2+
import { VideoId } from "./VideoId";
3+
import { VideoName } from "./VideoName";
4+
5+
export class Video {
6+
public constructor(
7+
public readonly id: VideoId,
8+
public readonly name: VideoName | undefined,
9+
public readonly duration: number | undefined
10+
) {}
11+
12+
public toPrimitives(): Primitives<Video> {
13+
return {
14+
id: this.id.value,
15+
name: this.name?.value,
16+
duration: this.duration,
17+
};
18+
}
19+
}

tests/VideoId.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { StringValueObject } from "./StringValueObject";
2+
3+
export class VideoId extends StringValueObject {}

tests/VideoName.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { StringValueObject } from "./StringValueObject";
2+
3+
export class VideoName extends StringValueObject {}

0 commit comments

Comments
 (0)