Skip to content

Commit 33a14ae

Browse files
committed
better listing and test structure
1 parent 0db9091 commit 33a14ae

9 files changed

+77
-70
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
dist
33
coverage
44
reports
5+
.eslintrc.js

.eslintrc.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ module.exports = {
66
"eslint-comments",
77
"jest",
88
"promise",
9-
"unicorn",
9+
"unicorn"
1010
],
1111
parserOptions: {
12-
project: "./tsconfig.json",
12+
project: "./tsconfig.json"
1313
},
1414
extends: [
1515
"airbnb-typescript/base",
@@ -18,21 +18,22 @@ module.exports = {
1818
"plugin:jest/recommended",
1919
"plugin:promise/recommended",
2020
"prettier",
21-
"prettier/@typescript-eslint",
21+
"prettier/@typescript-eslint"
2222
],
2323
rules: {
24+
"max-classes-per-file": ["error", 3],
2425
"import/prefer-default-export": "off",
2526
"no-use-before-define": [
2627
"error",
27-
{ functions: false, classes: true, variables: true },
28+
{ functions: false, classes: true, variables: true }
2829
],
2930
"@typescript-eslint/explicit-function-return-type": [
3031
"error",
31-
{ allowExpressions: true, allowTypedFunctionExpressions: true },
32+
{ allowExpressions: true, allowTypedFunctionExpressions: true }
3233
],
3334
"@typescript-eslint/no-use-before-define": [
3435
"error",
35-
{ functions: false, classes: true, variables: true, typedefs: true },
36-
],
37-
},
36+
{ functions: false, classes: true, variables: true, typedefs: true }
37+
]
38+
}
3839
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"changelog:update": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
2727
"clean": "(rm -r ./coverage || true) && (rm -r ./dist || true)",
2828
"clean:all": "npm run clean && (rm -r ./node_modules || true)",
29-
"lint": "eslint src/**/*.ts test/**/*.ts",
29+
"lint": "eslint src/**/*.ts",
3030
"test": "jest",
3131
"test:watch": "jest --watch",
3232
"test:mutations": "stryker run",

src/Scalars/FloatScalar.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77

88
export const FloatScalar: ValueObjectStatic<number> = class FloatScalar {
99
type: string;
10+
1011
value: number;
1112

1213
constructor(value: number) {
@@ -16,7 +17,7 @@ export const FloatScalar: ValueObjectStatic<number> = class FloatScalar {
1617
enforceExtension(this, FloatScalar);
1718
}
1819

19-
public static fromNative(value: number) {
20+
public static fromNative(value: number): ValueObject<number> {
2021
return new this(value);
2122
}
2223

src/Scalars/StringScalar.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77

88
export const StringScalar: ValueObjectStatic<string> = class StringScalar {
99
type: string;
10+
1011
value: string;
1112

1213
constructor(value: string) {
@@ -16,7 +17,7 @@ export const StringScalar: ValueObjectStatic<string> = class StringScalar {
1617
enforceExtension(this, StringScalar);
1718
}
1819

19-
public static fromNative(value: string) {
20+
public static fromNative(value: string): ValueObject<string> {
2021
return new this(value);
2122
}
2223

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { StringScalar } from "../StringScalar";
2+
3+
describe("Test StringScalar", () => {
4+
const testString = "Test String";
5+
class TESTString1 extends StringScalar {}
6+
const testStringClass = new TESTString1(testString);
7+
8+
test("must be extended (not instantiated)", () => {
9+
expect(() => {
10+
return new StringScalar(testString);
11+
}).toThrow(
12+
"StringScalar cannot be instantiated, you should create your own domain value objects that extend it"
13+
);
14+
});
15+
16+
test("fromNative() initialises object with correct type", () => {
17+
const obj = TESTString1.fromNative(testString);
18+
expect(obj.type).toBe("TESTString1");
19+
});
20+
21+
test(`toNative() returns "${testString}"`, () => {
22+
expect(testStringClass.toNative()).toBe(testString);
23+
});
24+
25+
test("isSame() returns true when values and type match", () => {
26+
expect(testStringClass.isSame(new TESTString1(testString))).toBeTruthy();
27+
});
28+
29+
test("isSame() returns false when values mismatch and type match", () => {
30+
expect(
31+
testStringClass.isSame(new TESTString1("Not the same"))
32+
).not.toBeTruthy();
33+
});
34+
35+
test("isSame() returns false when values match and type mismatch", () => {
36+
class TestStringClass2 extends StringScalar {}
37+
expect(
38+
testStringClass.isSame(new TestStringClass2(testString))
39+
).not.toBeTruthy();
40+
});
41+
});

src/__tests__/ValueObject.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ValueObject, getValueObjectType } from "../ValueObject";
2+
3+
class Stub implements ValueObject<string> {
4+
type = "TYPE";
5+
6+
value = "some value";
7+
8+
isSame = (): boolean => {
9+
return false;
10+
};
11+
12+
toNative = (): string => {
13+
return this.value;
14+
};
15+
}
16+
17+
describe("Test getValueObjectType()", () => {
18+
test("should return 'Stub' for stubbed class", () => {
19+
expect(getValueObjectType(new Stub())).toBe("Stub");
20+
});
21+
});

test/Scalars/StringScalar.test.ts

-40
This file was deleted.

test/ValueObject.test.ts

-19
This file was deleted.

0 commit comments

Comments
 (0)