@stancer/stancer 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/api-endpoint/http/fetch-http-client.d.ts.map +1 -1
  3. package/dist/api-endpoint/http/fetch-http-client.js +7 -1
  4. package/dist/api-endpoint/http/fetch-http-client.js.map +1 -1
  5. package/dist/core/common/handler/metadata-handler.d.ts +18 -0
  6. package/dist/core/common/handler/metadata-handler.d.ts.map +1 -0
  7. package/dist/core/common/handler/metadata-handler.js +19 -0
  8. package/dist/core/common/handler/metadata-handler.js.map +1 -0
  9. package/dist/event/model.d.ts +20 -0
  10. package/dist/event/model.d.ts.map +1 -0
  11. package/dist/event/model.js +30 -0
  12. package/dist/event/model.js.map +1 -0
  13. package/dist/payment-intent/api-endpoint.d.ts +16 -1
  14. package/dist/payment-intent/api-endpoint.d.ts.map +1 -1
  15. package/dist/payment-intent/api-endpoint.js +2 -1
  16. package/dist/payment-intent/api-endpoint.js.map +1 -1
  17. package/dist/payment-intent/model.d.ts +3 -2
  18. package/dist/payment-intent/model.d.ts.map +1 -1
  19. package/dist/payment-intent/model.js +6 -0
  20. package/dist/payment-intent/model.js.map +1 -1
  21. package/dist/webhook/api-endpoint.d.ts +16 -0
  22. package/dist/webhook/api-endpoint.d.ts.map +1 -0
  23. package/dist/webhook/api-endpoint.js +33 -0
  24. package/dist/webhook/api-endpoint.js.map +1 -0
  25. package/dist/webhook/model.d.ts +35 -0
  26. package/dist/webhook/model.d.ts.map +1 -0
  27. package/dist/webhook/model.js +110 -0
  28. package/dist/webhook/model.js.map +1 -0
  29. package/dist/webhook/repository.d.ts +7 -0
  30. package/dist/webhook/repository.d.ts.map +1 -0
  31. package/dist/webhook/repository.js +10 -0
  32. package/dist/webhook/repository.js.map +1 -0
  33. package/dist/webhook/validator.d.ts +4 -0
  34. package/dist/webhook/validator.d.ts.map +1 -0
  35. package/dist/webhook/validator.js +16 -0
  36. package/dist/webhook/validator.js.map +1 -0
  37. package/package.json +4 -2
  38. package/src/api-endpoint/http/fetch-http-client.ts +8 -1
  39. package/src/core/common/handler/metadata-handler.ts +19 -0
  40. package/src/event/model.ts +42 -0
  41. package/src/payment-intent/api-endpoint.ts +2 -1
  42. package/src/payment-intent/model.ts +8 -3
  43. package/src/webhook/api-endpoint.ts +36 -0
  44. package/src/webhook/model.ts +140 -0
  45. package/src/webhook/repository.ts +14 -0
  46. package/src/webhook/validator.ts +18 -0
  47. package/dist/main.d.ts +0 -2
  48. package/dist/main.d.ts.map +0 -1
  49. package/dist/main.js +0 -2
  50. package/dist/main.js.map +0 -1
  51. package/reports/base.css +0 -362
  52. package/reports/block-navigation.js +0 -75
  53. package/reports/cobertura-coverage.xml +0 -18
  54. package/reports/favicon.png +0 -0
  55. package/reports/index.html +0 -177
  56. package/reports/junit.xml +0 -25
  57. package/reports/prettify.css +0 -101
  58. package/reports/prettify.js +0 -955
  59. package/reports/sort-arrow-sprite.png +0 -0
  60. package/reports/sorter.js +0 -205
  61. package/test/fixtures/endpoint.stub.ts +0 -22
  62. package/test/fixtures/list-endpoint.stub.ts +0 -33
  63. package/test/fixtures/list-model.stub.ts +0 -24
  64. package/test/fixtures/model.stub.ts +0 -12
  65. package/test/functional/address.test.ts +0 -126
  66. package/test/functional/card.test.ts +0 -175
  67. package/test/functional/customer.test.ts +0 -174
  68. package/test/functional/payment-intent.test.ts +0 -215
  69. package/test/functional/payment.test.ts +0 -206
  70. package/test/functional/refund.test.ts +0 -112
  71. package/test/functional/sepa.test.ts +0 -136
  72. package/test/integration/stub-integration.test.ts +0 -88
  73. package/test/stub/stub-endpoint.ts +0 -42
  74. package/test/stub/stub-model.ts +0 -82
  75. package/test/stub/stub-repository.ts +0 -11
  76. package/test/test-helper.ts +0 -133
  77. package/test/unit/address/api-endpoint.test.ts +0 -134
  78. package/test/unit/address/fixtures.ts +0 -60
  79. package/test/unit/address/model.test.ts +0 -75
  80. package/test/unit/address/repository.test.ts +0 -148
  81. package/test/unit/address/validator.test.ts +0 -49
  82. package/test/unit/api-endpoint/http/fetch-http-client.test.ts +0 -44
  83. package/test/unit/api-endpoint/http/http.test.ts +0 -152
  84. package/test/unit/card/api-endpoint.test.ts +0 -170
  85. package/test/unit/card/card-helper.ts +0 -116
  86. package/test/unit/card/fixtures.ts +0 -87
  87. package/test/unit/card/model.test.ts +0 -167
  88. package/test/unit/card/repository.test.ts +0 -146
  89. package/test/unit/card/validator.test.ts +0 -96
  90. package/test/unit/core/common/mutation-tracked-object.test.ts +0 -24
  91. package/test/unit/customer/api-endpoint.test.ts +0 -143
  92. package/test/unit/customer/fixtures.ts +0 -73
  93. package/test/unit/customer/model.test.ts +0 -90
  94. package/test/unit/customer/repository.test.ts +0 -148
  95. package/test/unit/customer/validator.test.ts +0 -62
  96. package/test/unit/payment/api-endpoint.test.ts +0 -188
  97. package/test/unit/payment/fixtures.ts +0 -116
  98. package/test/unit/payment/model.test.ts +0 -240
  99. package/test/unit/payment/repository.test.ts +0 -270
  100. package/test/unit/payment/validator.test.ts +0 -137
  101. package/test/unit/payment-intent/api-endpoint.test.ts +0 -142
  102. package/test/unit/payment-intent/fixtures.ts +0 -94
  103. package/test/unit/payment-intent/model.test.ts +0 -157
  104. package/test/unit/payment-intent/repository.test.ts +0 -263
  105. package/test/unit/payment-intent/validator.test.ts +0 -136
  106. package/test/unit/refund/api-endpoint.test.ts +0 -129
  107. package/test/unit/refund/fixtures.ts +0 -58
  108. package/test/unit/refund/model.test.ts +0 -89
  109. package/test/unit/refund/repository.test.ts +0 -157
  110. package/test/unit/refund/validator.test.ts +0 -89
  111. package/test/unit/sepa/api-endpoint.test.ts +0 -170
  112. package/test/unit/sepa/fixture.ts +0 -62
  113. package/test/unit/sepa/model.test.ts +0 -86
  114. package/test/unit/sepa/repository.test.ts +0 -149
  115. package/test/unit/sepa/sepa-helper.ts +0 -71
  116. package/test/unit/sepa/validator.test.ts +0 -58
  117. package/test/unit/stub/stub-endpoint.test.ts +0 -236
  118. package/test/unit/stub/stub-list-endpoint.test.ts +0 -86
  119. package/test/unit/stub/stub-model.test.ts +0 -111
  120. package/test/unit/stub/stub-repository.test.ts +0 -144
@@ -1,96 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { FundingCardEnum } from '#card/enum/funding.js';
4
- import { NatureCardEnum } from '#card/enum/nature.js';
5
- import { NetworkCardEnum, PreferredNetworkCardEnum } from '#card/enum/network.js';
6
- import { CardModel } from '#card/model.js';
7
- import { cardValidator } from '#card/validator.js';
8
- import { checkType, goldilocks, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
9
-
10
- import { IdCustomer } from '../../../src/customer/model';
11
- import { randomCardNumber } from './card-helper';
12
-
13
- describe('Goldilocks', () => {
14
- const card = new CardModel(cardValidator);
15
-
16
- it('country', () => {
17
- goldilocks((value: string) => (card._data.country = value), 'string', { min: 2, max: 2 });
18
- checkType((value: string) => (card._data.country = value), ['string'], randomString(2, 2));
19
- });
20
- it('customer', () => {
21
- const falseCustomer = randomString(24);
22
- const trueCustomer = `cust_${randomString(24)}` as IdCustomer;
23
- expect(() => card.setCustomer(falseCustomer)).toThrow();
24
- card.setCustomer(trueCustomer);
25
- expect(card.customer).toBe(trueCustomer);
26
- });
27
- it('cvc', () => {
28
- const cvc = String(randomNumber(0, 999)).padStart(3, '0');
29
- expect(() => card.setCvc(cvc.padStart(4, '0'))).toThrow();
30
- expect(() => card.setCvc(String(randomNumber(0, 99)).padStart(2, '0'))).toThrow();
31
- expect(() => card.setCvc(randomString(3))).toThrow();
32
-
33
- checkType((value: string) => card.setCvc(value), ['string'], cvc);
34
- });
35
-
36
- it('expMonth', () => {
37
- checkType((value: number) => card.setExpMonth(value), ['number'], randomMonth());
38
- goldilocks((value: number) => card.setExpMonth(value), 'number', { min: 1, max: 12 });
39
- });
40
-
41
- it('expYear', () => {
42
- checkType((value: number) => card.setExpYear(value), ['number'], randomNumber(1000, 9999));
43
- goldilocks((value: number) => card.setExpYear(value), 'number', { min: 1000, max: 9999 });
44
- });
45
-
46
- it('externalId', () => {
47
- goldilocks((value: string) => card.setExternalId(value), 'string', { min: 1, max: 36 });
48
- checkType((value: string) => card.setExternalId(value), ['string'], randomString(1, 36));
49
- });
50
-
51
- it.each(FundingCardEnum.map((n) => [n]))('funding', (funding) => {
52
- const falseFunding = randomString(123, 188);
53
- expect(() => (card._data.funding = falseFunding)).toThrow();
54
- card._data.funding = funding;
55
- expect(card.funding).toBe(funding);
56
- });
57
-
58
- it('name', () => {
59
- goldilocks((value: string) => card.setName(value), 'string', { min: 1, max: 64 });
60
- checkType((value: string) => card.setName(value), ['string'], randomString(1, 64));
61
- });
62
-
63
- it.each(NatureCardEnum.map((n) => [n]))('nature', (nature) => {
64
- const falseNature = randomString(123, 188);
65
- expect(() => (card._data.nature = falseNature)).toThrow();
66
- card._data.nature = nature;
67
- expect(card.nature).toBe(nature);
68
- });
69
-
70
- it.each(NetworkCardEnum.map((n) => [n]))('network', (network) => {
71
- const falseNetwork = randomString(123, 188);
72
- expect(() => (card._data.network = falseNetwork)).toThrow();
73
- card._data.network = network;
74
- expect(card.network).toBe(network);
75
- });
76
-
77
- it('number', () => {
78
- const falseCardNumber = String(Number(randomCardNumber()) + 1);
79
- expect(() => card.setNumber(falseCardNumber)).toThrow();
80
- const trueCardNumber = randomCardNumber();
81
- card.setNumber(trueCardNumber);
82
- expect(card._data.number).toBe(trueCardNumber);
83
- });
84
-
85
- it.each(PreferredNetworkCardEnum.map((n) => [n]))('preferredNetwork', (network) => {
86
- const falsePreferredNetwork = randomString(123, 188);
87
- expect(() => card.setPreferredNetwork(falsePreferredNetwork)).toThrow();
88
- card.setPreferredNetwork(network);
89
- expect(card.preferredNetwork).toBe(network);
90
- });
91
-
92
- it('zipCode', () => {
93
- goldilocks((value: string) => card.setZipCode(value), 'string', { min: 0, max: 8 });
94
- checkType((value: string) => card.setZipCode(value), ['string'], randomString(0, 8));
95
- });
96
- });
@@ -1,24 +0,0 @@
1
- import { describe, it } from 'vitest';
2
- import { expect } from 'vitest';
3
-
4
- import { trackObjectMutations } from '#core/common/mutation-tracked-object.ts';
5
-
6
- describe('mutation tracked object', () => {
7
- it('set new data in mutation', () => {
8
- const validator = () => true;
9
- const data = {};
10
- const tracked = trackObjectMutations(data, validator);
11
- tracked['test'] = 'test';
12
- expect(tracked.getMutations()).toStrictEqual({ test: 'test' });
13
- });
14
-
15
- it('modify initialized data ', () => {
16
- const validator = () => true;
17
- const data = { truc: '12', machin: 'x' };
18
- const tracked = trackObjectMutations(data, validator);
19
- expect(tracked.getMutations()).toStrictEqual({});
20
- expect(tracked.toRaw()).toStrictEqual(data);
21
- tracked['truc'] = 'machin';
22
- expect(tracked.getMutations()).toStrictEqual({ truc: 'machin' });
23
- });
24
- });
@@ -1,143 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { StancerError } from '#core/error/base-error.js';
4
- import { CustomerApiEndpoint } from '#customer/api-endpoint.js';
5
- import type { CustomerData, IdCustomer } from '#customer/model.js';
6
- import { randomString } from '#test/test-helper.js';
7
-
8
- import { endpointResource, endpointResourceDeserialized } from './fixtures.js';
9
-
10
- vi.mock('#api/http/http.js', async (importHTTP) => {
11
- const original: Record<string, Promise<unknown>> = await importHTTP();
12
- return {
13
- ...original,
14
- transformResponseToData: vi.fn((value) => value.json()),
15
- };
16
- });
17
-
18
- vi.mock('#api/http/fetch-http-client.js', () => {
19
- return {
20
- httpClient: {
21
- makeRequest: vi.fn(() => {
22
- return Promise.resolve({
23
- status: 200,
24
- json: () => {
25
- return {
26
- ...endpointResource,
27
- };
28
- },
29
- });
30
- }),
31
- },
32
- };
33
- });
34
-
35
- describe('customer api endpoint', () => {
36
- describe('Serialize/Deserialize resources', () => {
37
- const endpoint = new CustomerApiEndpoint();
38
-
39
- it('test deserialize resource', () => {
40
- const deserializedResources = endpoint.deserializeResource(endpointResource);
41
- //Double match to guaranty exact same shape
42
- expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
43
- expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
44
- });
45
-
46
- it('test post serialize resource', () => {
47
- const serializedResources = endpoint.postObjectSerializer(
48
- endpointResourceDeserialized,
49
- ) as typeof endpointResource;
50
- // property not in postObject are API generated or not postable
51
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
52
- const { deleted, created, id, ...postObject } = endpointResource;
53
- //Double match to guaranty exact same shape
54
- expect(serializedResources).toMatchObject(postObject);
55
- expect(postObject).toMatchObject(serializedResources);
56
- });
57
-
58
- it('test patch serialize resource (not available in repository)', () => {
59
- const serializedResources = endpoint.patchObjectSerializer(
60
- endpointResourceDeserialized,
61
- ) as typeof endpointResource;
62
- // property not in patchObject are API generated or not patchable
63
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
64
- const { deleted, created, id, ...patchObject } = endpointResource;
65
- //Double match to guaranty exact same shape
66
- expect(serializedResources).toMatchObject(patchObject);
67
- expect(patchObject).toMatchObject(serializedResources);
68
- });
69
- });
70
-
71
- describe('endpoint path', () => {
72
- it('endpoint url', () => {
73
- const endpoint = new CustomerApiEndpoint();
74
- const id = ('cust_' + randomString(24)) as IdCustomer;
75
- expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/customers/${id}`);
76
- expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/customers/');
77
- });
78
- });
79
-
80
- describe('verbs', () => {
81
- const customerEndpoint = new CustomerApiEndpoint();
82
-
83
- it('get', async () => {
84
- const responseWithId = await customerEndpoint.get(('cust_' + randomString(24)) as IdCustomer);
85
- expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
86
- });
87
-
88
- it('post', async () => {
89
- const responseWithId = await customerEndpoint.post(endpointResource);
90
- expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
91
- });
92
-
93
- it('patch', async () => {
94
- const patchResponse = await customerEndpoint.patch(('cust_' + randomString(24)) as IdCustomer, endpointResource);
95
- expect(patchResponse).toStrictEqual(endpointResourceDeserialized);
96
- });
97
-
98
- it('delete', async () => {
99
- expect(await customerEndpoint.delete(('cust_' + randomString(24)) as IdCustomer)).toStrictEqual(
100
- endpointResourceDeserialized,
101
- );
102
- });
103
- });
104
-
105
- describe('send', () => {
106
- const { mobile, email, country } = endpointResourceDeserialized;
107
-
108
- it.each([
109
- ['mobile', mobile],
110
- ['email', email],
111
- ])('send with partial required data', async (valueName, requiredValue) => {
112
- const endpoint = new CustomerApiEndpoint();
113
- const mutationsData = {} as Partial<CustomerData>;
114
- mutationsData[valueName] = requiredValue;
115
- await expect(endpoint.send(undefined, mutationsData)).resolves.toStrictEqual(endpointResourceDeserialized);
116
- });
117
-
118
- it('does not post with missing partially required data', async () => {
119
- const endpoint = new CustomerApiEndpoint();
120
- expect(() => endpoint.send(undefined, { country })).toThrow(StancerError);
121
- });
122
-
123
- it('Patch if id', async () => {
124
- const endpoint = new CustomerApiEndpoint();
125
- const spyonPatch = vi.spyOn(endpoint, 'patch');
126
- const spyonPost = vi.spyOn(endpoint, 'post');
127
- await expect(endpoint.send('cust_' + randomString(24), { mobile, country })).resolves.toStrictEqual(
128
- endpointResourceDeserialized,
129
- );
130
- expect(spyonPatch).toHaveBeenCalledTimes(1);
131
- expect(spyonPost).toHaveBeenCalledTimes(0);
132
- });
133
-
134
- it('post if no id', () => {
135
- const endpoint = new CustomerApiEndpoint();
136
- const spyonPatch = vi.spyOn(endpoint, 'patch');
137
- const spyonPost = vi.spyOn(endpoint, 'post');
138
- endpoint.send(undefined, { email });
139
- expect(spyonPatch).toHaveBeenCalledTimes(0);
140
- expect(spyonPost).toHaveBeenCalledTimes(1);
141
- });
142
- });
143
- });
@@ -1,73 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
2
-
3
- import { IdCustomer } from '#customer/model.js';
4
- import { randomMonth, randomNumber, randomString } from '#test/test-helper.js';
5
-
6
- import { IdAddress } from '../../../src/address/temporary-types';
7
-
8
- const billing_address = 'addr_' + randomString(24);
9
- const country = randomString(2);
10
- const created = randomNumber(1786460000, 2000000000);
11
- const date_birth = `${randomNumber(1000, 2025)}-${randomMonth().toString().padStart(2, '0')}-${randomNumber(1, 28).toString().padStart(2, '0')}`;
12
- const deleted = false;
13
- const email = randomString(1, 64);
14
- const external_id = randomString(1, 36);
15
- const id = `cust_${randomString(24)}`;
16
- const legal_id = randomString(1, 16);
17
- const mobile = randomString(4, 16);
18
- const name = randomString(3, 64);
19
- const shipping_address = 'addr_' + randomString(24);
20
-
21
- export const endpointResource = {
22
- billing_address,
23
- country,
24
- created,
25
- date_birth,
26
- deleted,
27
- email,
28
- external_id,
29
- id,
30
- legal_id,
31
- mobile,
32
- name,
33
- shipping_address,
34
- };
35
-
36
- export const endpointResourceDeserialized = {
37
- createdAt: Temporal.Instant.fromEpochMilliseconds(created * 1000),
38
-
39
- billingAddress: billing_address as IdAddress,
40
- country,
41
- dateBirth: Temporal.PlainDate.from(date_birth),
42
- deleted,
43
- email,
44
- externalId: external_id,
45
- id: id as IdCustomer,
46
- legalId: legal_id,
47
- mobile,
48
- name,
49
- shippingAddress: shipping_address as IdAddress,
50
- };
51
-
52
- export const creationResource = {
53
- billingAddress: billing_address,
54
- country,
55
- dateBirth: Temporal.PlainDate.from(date_birth),
56
- email,
57
- externalId: external_id,
58
- id,
59
- legalId: legal_id,
60
- mobile,
61
- name,
62
- shippingAddress: shipping_address,
63
- };
64
- export const listResource = [
65
- {
66
- ...endpointResource,
67
- name: 'item1',
68
- },
69
- {
70
- ...endpointResource,
71
- name: 'item2',
72
- },
73
- ];
@@ -1,90 +0,0 @@
1
- import { describe, it } from 'vitest';
2
-
3
- import { IdAddress, idAddress } from '#address/model.ts';
4
- import { Validator } from '#core/validator.js';
5
- import { CustomerData, CustomerModel } from '#customer/model.js';
6
- import { SetterGetter, randomString } from '#test/test-helper.js';
7
-
8
- describe('Customer Class', () => {
9
- describe('Setter & Getter', () => {
10
- const customer = new CustomerModel((() => true) as unknown as Validator<CustomerData>);
11
-
12
- it('country', () => {
13
- SetterGetter(
14
- (value: string) => customer.setCountry(value),
15
- () => customer.country,
16
- randomString(2),
17
- );
18
- });
19
-
20
- it('email', () => {
21
- SetterGetter(
22
- (value: string) => customer.setEmail(value),
23
- () => customer.email,
24
- randomString(30),
25
- );
26
- });
27
-
28
- it('externalId', () => {
29
- SetterGetter(
30
- (value: string) => customer.setExternalId(value),
31
- () => customer.externalId,
32
- randomString(30),
33
- );
34
- });
35
-
36
- it('legalId', () => {
37
- SetterGetter(
38
- (value: string) => customer.setLegalId(value),
39
- () => customer.legalId,
40
- randomString(15),
41
- );
42
- });
43
-
44
- it('mobile', () => {
45
- SetterGetter(
46
- (value: string) => customer.setMobile(value),
47
- () => customer.mobile,
48
- randomString(12),
49
- );
50
- });
51
-
52
- it('name', () => {
53
- SetterGetter(
54
- (value: string) => customer.setName(value),
55
- () => customer.name,
56
- randomString(12),
57
- );
58
- });
59
-
60
- it('billing Address', () => {
61
- SetterGetter(
62
- (value: IdAddress) => customer.setBillingAddress(value),
63
- () => customer.billingAddress,
64
- idAddress(`addr_${randomString(24)}`),
65
- );
66
- });
67
-
68
- it('Shipping Address', () => {
69
- SetterGetter(
70
- (value: IdAddress) => customer.setShippingAddress(value),
71
- () => customer.shippingAddress,
72
- idAddress(`addr_${randomString(24)}`),
73
- );
74
- });
75
-
76
- it('deleted (unsettable)', () => {
77
- SetterGetter(
78
- (value: boolean) => (customer._data.deleted = value),
79
- () => customer.deleted,
80
- true,
81
- );
82
- customer._data.deleted = null;
83
- SetterGetter(
84
- (value: boolean) => (customer._data.deleted = value),
85
- () => customer.deleted,
86
- false,
87
- );
88
- });
89
- });
90
- });
@@ -1,148 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { AbstractRepository } from '#core/abstract-repository.js';
4
- import { CustomerApiEndpoint } from '#customer/api-endpoint.js';
5
- import { CustomerModel, IdCustomer } from '#customer/model.js';
6
- import { CustomerRepository } from '#customer/repository.js';
7
- import { customerValidator } from '#customer/validator.js';
8
- import { StancerError } from '#error/base-error.js';
9
- import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
10
- import { randomString } from '#test/test-helper.js';
11
-
12
- import { creationResource, endpointResource, listResource } from './fixtures.js';
13
-
14
- const idPrefix = 'cust';
15
- const id = `${idPrefix}_${randomString(24)}` as IdCustomer;
16
- const stancerErrorID = `${idPrefix}_unidentifiedError${randomString(7)}`;
17
- const stancerRouteInaccessibleId = `${idPrefix}_StancerRouteInnacessible`;
18
- const dataHasBeenSentId = `${idPrefix}_DataHasBeenSent${randomString(9)}`;
19
- const dataUnmodified = `${idPrefix}_unmodifiedData${randomString(10)}`;
20
- class MockApiEndpoint {
21
- constructor() {}
22
- list() {
23
- async function* listGenerator() {
24
- yield* listResource;
25
- }
26
- return listGenerator();
27
- }
28
-
29
- delete() {
30
- return endpointResource;
31
- }
32
-
33
- send(id: string, data: Record<string, unknown>) {
34
- if (id) {
35
- if (id === stancerErrorID) {
36
- throw new StancerError();
37
- }
38
- if (id === stancerRouteInaccessibleId) {
39
- throw new StancerRouteInaccessibleError('PATCH', 'stub');
40
- }
41
- if (id === dataUnmodified) {
42
- return;
43
- }
44
- }
45
- return { ...data, id: dataHasBeenSentId };
46
- }
47
-
48
- get() {
49
- return endpointResource;
50
- }
51
- }
52
-
53
- it('can be instanciated', () => {
54
- const instance = new CustomerRepository();
55
- expect(instance).toBeInstanceOf(CustomerRepository);
56
- expect(instance).toBeInstanceOf(AbstractRepository);
57
- //accessing private method & property
58
- expect(instance.endpoint).toBeInstanceOf(CustomerApiEndpoint);
59
- expect(instance.validator).toBe(customerValidator);
60
- expect(instance.staticModel).toBe(CustomerModel);
61
- });
62
-
63
- describe('repository', () => {
64
- const mockRepo = new MockApiEndpoint() as unknown as CustomerApiEndpoint; // I lie, for convenience sake
65
- const repository = new CustomerRepository(mockRepo);
66
-
67
- describe('create', () => {
68
- it("doesn't accept bad data", async () => {
69
- const badID = randomString(30, 56);
70
- // using puposefuly bad ID
71
- await expect(async () => await repository.create(badID)).rejects.toThrow();
72
- });
73
-
74
- it('accept good data', async () => {
75
- const model = await repository.create(id);
76
- expect(model).toBeInstanceOf(CustomerModel);
77
- });
78
- });
79
-
80
- it('delete', async () => {
81
- const modelWithoutId = await repository.create();
82
- await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
83
- const model = await repository.create({ id: endpointResource.id, ...creationResource });
84
- await expect((await repository.delete(model))._data).toStrictEqual(endpointResource);
85
- });
86
-
87
- describe('send', () => {
88
- it('should return model', async () => {
89
- const model = await repository.create(creationResource);
90
- const sentModel = await repository.send(model);
91
- expect(sentModel).toStrictEqual(model);
92
- expect(sentModel._data.id).toEqual(dataHasBeenSentId);
93
- });
94
-
95
- it('should throw on inprecise error', async () => {
96
- const model = await repository.create({ ...creationResource, id: stancerErrorID as IdCustomer });
97
- await expect(() => repository.send(model)).rejects.toThrow(StancerError);
98
- });
99
-
100
- it('should return error on StancerRouteInacessibleError', async () => {
101
- const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdCustomer });
102
- await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
103
- });
104
-
105
- it('should return unmodified model when no new Data', async () => {
106
- const model = await repository.create({ ...creationResource, id: dataUnmodified as IdCustomer });
107
- const sentModel = await repository.send(model);
108
- expect(model).toStrictEqual(sentModel);
109
- expect(sentModel._data.id).toEqual(dataUnmodified);
110
- });
111
- });
112
-
113
- describe('populate', () => {
114
- it('should not modify model if no Id', async () => {
115
- const getEndpoint = vi.spyOn(mockRepo, 'get');
116
- const model = await repository.create(creationResource);
117
- expect(model).toBeInstanceOf(CustomerModel);
118
- expect(getEndpoint).toHaveBeenCalledTimes(0);
119
- });
120
-
121
- it('should get model if only id', async () => {
122
- const getEndpoint = vi.spyOn(mockRepo, 'get');
123
- const model = await repository.create(id);
124
- expect(model).toBeInstanceOf(CustomerModel);
125
- expect(getEndpoint).toHaveBeenCalledTimes(1);
126
- });
127
- });
128
-
129
- describe('list', async () => {
130
- it('return correct model one by one', async () => {
131
- const listStub = repository.list();
132
- const item1 = await listStub.next();
133
- expect(item1.done).toBeFalsy();
134
- expect(item1.value).toHaveProperty('id');
135
- expect(item1.value).toBeInstanceOf(CustomerModel);
136
- expect(item1.value.name).toBe('item1');
137
-
138
- const item2 = await listStub.next();
139
- expect(item2.done).toBeFalsy();
140
- expect(item2.value).toHaveProperty('id');
141
- expect(item2.value).toBeInstanceOf(CustomerModel);
142
- expect(item2.value.name).toBe('item2');
143
-
144
- const item3 = await listStub.next();
145
- expect(item3.done).toBeTruthy();
146
- });
147
- });
148
- });
@@ -1,62 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
2
- import { describe, expect, it } from 'vitest';
3
-
4
- import { CustomerModel } from '#customer/model.js';
5
- import { customerValidator } from '#customer/validator.js';
6
- import { checkType, goldilocks, randomMonth, randomNumber, randomPastDate, randomString } from '#test/test-helper.js';
7
-
8
- describe('Goldilocks', () => {
9
- const customer = new CustomerModel(customerValidator);
10
-
11
- it('Country', () => {
12
- goldilocks((value: string) => customer.setCountry(value), 'string', { min: 2, max: 2 });
13
- checkType((value: string) => customer.setCountry(value), ['string'], randomString(2, 2));
14
- });
15
-
16
- it('Email', () => {
17
- goldilocks((value: string) => customer.setEmail(value), 'string', { min: 1, max: 64 });
18
- checkType((value: string) => customer.setEmail(value), ['string'], randomString(1, 64));
19
- });
20
-
21
- it('ExternalId', () => {
22
- goldilocks((value: string) => customer.setExternalId(value), 'string', { min: 1, max: 36 });
23
- checkType((value: string) => customer.setExternalId(value), ['string'], randomString(1, 36));
24
- });
25
-
26
- it('LegalId', () => {
27
- goldilocks((value: string) => customer.setLegalId(value), 'string', { min: 1, max: 16 });
28
- checkType((value: string) => customer.setLegalId(value), ['string'], randomString(1, 16));
29
- });
30
-
31
- it('Mobile', () => {
32
- goldilocks((value: string) => customer.setMobile(value), 'string', { min: 1, max: 16 });
33
- });
34
- it('Name', () => {
35
- goldilocks((value: string) => customer.setName(value), 'string', { min: 1, max: 64 });
36
- checkType((value: string) => customer.setName(value), ['string'], randomString(3, 64));
37
- });
38
-
39
- it('DateBirth', () => {
40
- const falseDateBirth = new Temporal.PlainDate(9999, randomMonth(), randomNumber(1, 30));
41
- const DateBirth = randomPastDate();
42
- expect(() => customer.setDateBirth(falseDateBirth)).toThrow();
43
- customer.setDateBirth(DateBirth);
44
- expect(customer.dateBirth).toBe(DateBirth);
45
- });
46
-
47
- it('billingAddress', () => {
48
- const falseAddress = randomString(1, 28);
49
- const trueAddress = `addr_${randomString(24)}`;
50
- expect(() => (customer._data.billingAddress = falseAddress)).toThrow();
51
- customer._data.billingAddress = trueAddress;
52
- expect(customer.billingAddress).toBe(trueAddress);
53
- });
54
-
55
- it('shippingAddress', () => {
56
- const falseAddress = randomString(1, 28);
57
- const trueAddress = `addr_${randomString(24)}`;
58
- expect(() => (customer._data.shippingAddress = falseAddress)).toThrow();
59
- customer._data.shippingAddress = trueAddress;
60
- expect(customer.shippingAddress).toBe(trueAddress);
61
- });
62
- });