@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,170 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { CardApiEndpoint } from '#card/api-endpoint.js';
4
- import type { IdCard } from '#card/model.js';
5
- import { randomExpYear, randomMonth, randomString } from '#test/test-helper.js';
6
-
7
- import { randomCardNumber } from './card-helper.js';
8
- import { creationResource, 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('card api endpoint', () => {
36
- describe('Serialize/Deserialize resources', () => {
37
- const endpoint = new CardApiEndpoint();
38
-
39
- it('test deserialize resource', () => {
40
- const deserializedResources = endpoint.deserializeResource(endpointResource);
41
-
42
- //Double match to guaranty exact same shape
43
- expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
44
- expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
45
- });
46
-
47
- it('test post serialize resource', () => {
48
- const serializedResources = endpoint.postObjectSerializer({
49
- ...endpointResourceDeserialized,
50
- number: creationResource.number,
51
- tokenize: creationResource.tokenize,
52
- cvc: creationResource.cvc,
53
- }) as typeof endpointResource;
54
- // The fields not included in the partial object are API generated
55
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
56
- const { brand, country, created, deleted, funding, id, last4, nature, network, ...partialObject } =
57
- endpointResource;
58
- const postObject = {
59
- ...partialObject,
60
- number: creationResource.number,
61
- tokenize: creationResource.tokenize,
62
- cvc: creationResource.cvc,
63
- };
64
- //Double match to guaranty exact same shape
65
- expect(serializedResources).toMatchObject(postObject);
66
- expect(postObject).toMatchObject(serializedResources);
67
- });
68
-
69
- it('test patch serialize resource (not available in repository)', () => {
70
- const serializedResources = endpoint.patchObjectSerializer(
71
- endpointResourceDeserialized,
72
- ) as typeof endpointResource;
73
- //The object with all the patchable field
74
- const patchObject = {
75
- exp_month: endpointResource.exp_month,
76
- exp_year: endpointResource.exp_year,
77
- external_id: endpointResource.external_id,
78
- zip_code: endpointResource.zip_code,
79
- };
80
- //Double match to guaranty exact same shape
81
- expect(serializedResources).toMatchObject(patchObject);
82
- expect(patchObject).toMatchObject(serializedResources);
83
- });
84
- });
85
-
86
- describe('endpoint path', () => {
87
- it('endpoint url', () => {
88
- const endpoint = new CardApiEndpoint();
89
- const id = ('card_' + randomString(24)) as IdCard;
90
- expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/cards/${id}`);
91
- expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/cards/');
92
- });
93
- });
94
-
95
- describe('verbs', () => {
96
- const cardEndpoint = new CardApiEndpoint();
97
-
98
- it('get', async () => {
99
- const responseWithId = await cardEndpoint.get(('card_' + randomString(24)) as IdCard);
100
- expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
101
- });
102
-
103
- it('post no deserialization', async () => {
104
- const responseWithId = await cardEndpoint.post({
105
- id: 'card_' + randomString(24),
106
- expMonth: randomMonth(),
107
- expYear: randomExpYear(),
108
- number: randomCardNumber(),
109
- });
110
- expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
111
- });
112
-
113
- it('patch', async () => {
114
- const patchResponse = await cardEndpoint.patch(('card_' + randomString(24)) as IdCard, endpointResource);
115
- expect(patchResponse).toStrictEqual(endpointResourceDeserialized);
116
- });
117
-
118
- it('delete', async () => {
119
- expect(await cardEndpoint.delete(('card_' + randomString(24)) as IdCard)).toStrictEqual(
120
- endpointResourceDeserialized,
121
- );
122
- });
123
- });
124
-
125
- describe('send', () => {
126
- const { expYear, expMonth } = endpointResourceDeserialized;
127
-
128
- it('send with required data', async () => {
129
- const endpoint = new CardApiEndpoint();
130
- await expect(endpoint.send(undefined, { expYear, expMonth, number: randomCardNumber() })).resolves.toStrictEqual(
131
- endpointResourceDeserialized,
132
- );
133
- });
134
-
135
- it('does not post with missing expYear', async () => {
136
- const endpoint = new CardApiEndpoint();
137
- expect(() => endpoint.send(undefined, { expMonth, number: randomCardNumber() })).toThrow();
138
- });
139
-
140
- it('does not post with missing expMonth', async () => {
141
- const endpoint = new CardApiEndpoint();
142
- expect(() => endpoint.send(undefined, { expYear, number: randomCardNumber() })).toThrow();
143
- });
144
-
145
- it('does not post with missing number', async () => {
146
- const endpoint = new CardApiEndpoint();
147
- expect(() => endpoint.send(undefined, { expYear, expMonth })).toThrow();
148
- });
149
-
150
- it('Patch if id', async () => {
151
- const endpoint = new CardApiEndpoint();
152
- const spyonPatch = vi.spyOn(endpoint, 'patch');
153
- const spyonPost = vi.spyOn(endpoint, 'post');
154
- await expect(endpoint.send('card_' + randomString(24), { expYear, expMonth })).resolves.toStrictEqual(
155
- endpointResourceDeserialized,
156
- );
157
- expect(spyonPatch).toHaveBeenCalledTimes(1);
158
- expect(spyonPost).toHaveBeenCalledTimes(0);
159
- });
160
-
161
- it('post if no id', () => {
162
- const endpoint = new CardApiEndpoint();
163
- const spyonPatch = vi.spyOn(endpoint, 'patch');
164
- const spyonPost = vi.spyOn(endpoint, 'post');
165
- endpoint.send(undefined, { expYear, expMonth, number: randomCardNumber() });
166
- expect(spyonPatch).toHaveBeenCalledTimes(0);
167
- expect(spyonPost).toHaveBeenCalledTimes(1);
168
- });
169
- });
170
- });
@@ -1,116 +0,0 @@
1
- import { randomNumber } from '#test/test-helper.js';
2
-
3
- export const randomCardNumbers = () => {
4
- // Card number found on https://www.freeformatter.com/credit-card-number-generator-validator.html
5
- const data = [];
6
-
7
- // VISA
8
- data.push('4532160583905253');
9
- data.push('4103344114503410');
10
- data.push('4716929813250776300');
11
-
12
- // MasterCard
13
- data.push('5312580044202748');
14
- data.push('2720995588028031');
15
- data.push('5217849688268117');
16
-
17
- // American Express (AMEX)
18
- data.push('370301138747716');
19
- data.push('340563568138644');
20
- data.push('371461161518951');
21
-
22
- // Discover
23
- data.push('6011651456571367');
24
- data.push('6011170656779399');
25
- data.push('6011693048292929421');
26
-
27
- // JCB
28
- data.push('3532433013111566');
29
- data.push('3544337258139297');
30
- data.push('3535502591342895821');
31
-
32
- // Diners Club - North America
33
- data.push('5480649643931654');
34
- data.push('5519243149714783');
35
- data.push('5509141180527803');
36
-
37
- // Diners Club - Carte Blanche
38
- data.push('30267133988393');
39
- data.push('30089013015810');
40
- data.push('30109478108973');
41
-
42
- // Diners Club - International
43
- data.push('36052879958170');
44
- data.push('36049904526204');
45
- data.push('36768208048819');
46
-
47
- // Maestro
48
- data.push('5893433915020244');
49
- data.push('6759761854174320');
50
- data.push('6759998953884124');
51
-
52
- // Visa Electron
53
- data.push('4026291468019846');
54
- data.push('4844059039871494');
55
- data.push('4913054050962393');
56
-
57
- // InstaPayment
58
- data.push('6385037148943057');
59
- data.push('6380659492219803');
60
- data.push('6381454097795863');
61
-
62
- // Classic one
63
- data.push('4111111111111111');
64
- data.push('4242424242424242');
65
- data.push('4444333322221111');
66
-
67
- return data;
68
- };
69
-
70
- export const randomCardNumber = () => {
71
- const data = randomCardNumbers();
72
- return data[randomNumber(0, data.length - 1)];
73
- };
74
-
75
- export const getValidCardNumber = (): string => {
76
- const cards = getCardBranded();
77
- const card = Object.values(cards).reduce((prev, curr) => prev.concat(curr));
78
-
79
- return card[randomNumber(0, card.length - 1)];
80
- };
81
-
82
- const getCardBranded = (): { visa: string[]; mastercard: string[]; national: string[] } => {
83
- return {
84
- visa: [
85
- '4000000400000008',
86
- '4000000560000004',
87
- '4000002080000001',
88
- '4000002460000001',
89
- '4000002760000016',
90
- '4000003720000005',
91
- '4000003800000008',
92
- '4000005280000002',
93
- '4000005780000007',
94
- '4000006200000007',
95
- '4000006430000009',
96
- '4000007240000007',
97
- '4000007520000008',
98
- '4000007560000009',
99
- '4000008260000000',
100
- '4242424242424242',
101
- '4444333322221111',
102
- // '4000000000003055', This card is only for card patching (see youtrack API-549)
103
- '4000000760000002',
104
- '4000001240000000',
105
- '4000004840000008',
106
- ],
107
- mastercard: [
108
- '5555555555554444',
109
- '5200828282828210',
110
- '5105105105105100',
111
- ],
112
- national: [
113
- '4000002500000003',
114
- ],
115
- };
116
- };
@@ -1,87 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
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 { idCard } from '#card/model.js';
7
- import { idCustomer } from '#customer/model.ts';
8
- import { randomCVC, randomExpYear, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
9
-
10
- import { randomCardNumber } from './card-helper';
11
-
12
- const id = `card_${randomString(24)}`;
13
-
14
- const brand = randomString(2, 10);
15
- const country = randomString(2);
16
- const customer = `cust_${randomString(24)}`;
17
- const cvc = randomCVC();
18
- const created = randomNumber(1786460000, 2000000000);
19
- const deleted = false;
20
- const expMonth = randomMonth();
21
- const expYear = randomExpYear();
22
- const externalId = randomString(1, 36);
23
- const funding = FundingCardEnum[randomNumber(0, FundingCardEnum.length - 1)];
24
- const number = randomCardNumber();
25
- const last4 = number.slice(number.length - 4);
26
- const name = randomString(1, 64);
27
- const nature = NatureCardEnum[randomNumber(0, NatureCardEnum.length - 1)];
28
- const network = NetworkCardEnum[randomNumber(0, NetworkCardEnum.length - 1)];
29
- const preferredNetwork = PreferredNetworkCardEnum[randomNumber(0, PreferredNetworkCardEnum.length - 1)];
30
- const tokenize = Boolean(randomNumber(0, 1));
31
- const zipCode = randomString(2, 8);
32
-
33
- export const endpointResource = {
34
- id,
35
- brand,
36
- country,
37
- customer,
38
- cvc,
39
- created,
40
- deleted,
41
- exp_month: expMonth,
42
- exp_year: expYear,
43
- external_id: externalId,
44
- funding,
45
- last4,
46
- name,
47
- nature,
48
- network,
49
- number,
50
- preferred_network: preferredNetwork,
51
- zip_code: zipCode,
52
- };
53
-
54
- export const endpointResourceDeserialized = {
55
- id: idCard(id),
56
- brand,
57
- country,
58
- customer: idCustomer(customer),
59
- createdAt: Temporal.Instant.fromEpochMilliseconds(created * 1000),
60
- deleted,
61
- expMonth,
62
- expYear,
63
- externalId,
64
- funding,
65
- last4,
66
- name,
67
- nature,
68
- network,
69
- preferredNetwork,
70
- zipCode,
71
- };
72
-
73
- export const creationResource = {
74
- cvc,
75
- number,
76
- tokenize,
77
- };
78
- export const listResource = [
79
- {
80
- ...endpointResource,
81
- name: 'item1',
82
- },
83
- {
84
- ...endpointResource,
85
- name: 'item2',
86
- },
87
- ];
@@ -1,167 +0,0 @@
1
- import { describe, 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 { CardData, CardModel } from '#card/model.js';
7
- import { Validator } from '#core/validator.js';
8
- import { IdCustomer } from '#customer/model.ts';
9
- import { SetterGetter, randomExpYear, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
10
-
11
- import { randomCardNumbers } from './card-helper';
12
-
13
- describe('card Class', () => {
14
- const card = new CardModel((() => true) as unknown as Validator<CardData>);
15
-
16
- describe('Setter & Getter', () => {
17
- it('brand', () => {
18
- SetterGetter(
19
- (value: string) => (card._data.brand = value),
20
- () => card.brand,
21
- randomString(30),
22
- );
23
- });
24
-
25
- it('deleted (unsettable)', () => {
26
- SetterGetter(
27
- (value: boolean) => (card._data.deleted = value),
28
- () => card.deleted,
29
- true,
30
- );
31
- card._data.deleted = null;
32
- SetterGetter(
33
- (value: boolean) => (card._data.deleted = value),
34
- () => card.deleted,
35
- false,
36
- );
37
- });
38
-
39
- it('country', () => {
40
- SetterGetter(
41
- (value: string) => (card._data.country = value),
42
- () => card.country,
43
- randomString(2),
44
- );
45
- });
46
-
47
- it('customer', () => {
48
- SetterGetter(
49
- (value: IdCustomer) => card.setCustomer(value),
50
- () => card.customer,
51
- ``,
52
- );
53
- });
54
-
55
- it('cvc', () => {
56
- SetterGetter(
57
- (value: string) => card.setCvc(value),
58
- () => card._data.cvc,
59
- randomString(3),
60
- );
61
- });
62
-
63
- it('expMonth', () => {
64
- SetterGetter(
65
- (value: number) => card.setExpMonth(value),
66
- () => card.expMonth,
67
- randomMonth(),
68
- );
69
- });
70
-
71
- it('expYear', () => {
72
- SetterGetter(
73
- (value: string) => card.setExpYear(value),
74
- () => card.expYear,
75
- randomExpYear(),
76
- );
77
- });
78
-
79
- it('externalId', () => {
80
- SetterGetter(
81
- (value: string) => card.setExternalId(value),
82
- () => card.externalId,
83
- randomString(1, 36),
84
- );
85
- });
86
-
87
- it.each([FundingCardEnum.map((value) => [value])])('funding', (value) => {
88
- SetterGetter(
89
- (value: (typeof FundingCardEnum)[number]) => (card._data.funding = value),
90
- () => card.funding,
91
- value,
92
- );
93
- });
94
-
95
- it('last4', () => {
96
- SetterGetter(
97
- (value: string) => (card._data.last4 = value),
98
- () => card.last4,
99
- String(randomNumber(0, 9999)).padStart(4, '0'),
100
- );
101
- });
102
-
103
- it('name', () => {
104
- SetterGetter(
105
- (value: string) => card.setName(value),
106
- () => card.name,
107
- randomString(1, 64),
108
- );
109
- });
110
-
111
- it.each([NatureCardEnum.map((value) => [value])])('nature', (value) => {
112
- SetterGetter(
113
- (value: (typeof NatureCardEnum)[number]) => (card._data.nature = value),
114
- () => card.nature,
115
- value,
116
- );
117
- });
118
-
119
- it.each([NetworkCardEnum.map((value) => [value])])('network', (value) => {
120
- SetterGetter(
121
- (value: (typeof NetworkCardEnum)[number]) => (card._data.network = value),
122
- () => card.network,
123
- value,
124
- );
125
- });
126
-
127
- it.each((randomCardNumbers() as string[]).map((value) => [value]))('number %s', (number) => {
128
- card._data.number = null;
129
- SetterGetter(
130
- (value: string) => card.setNumber(value),
131
- () => card._data.number,
132
- number,
133
- );
134
- });
135
-
136
- it.each(PreferredNetworkCardEnum.map((value) => [value]))('preferredNetwork %s', (value) => {
137
- card._data.preferredNetwork = null;
138
- SetterGetter(
139
- (value: (typeof PreferredNetworkCardEnum)[number]) => card.setPreferredNetwork(value),
140
- () => card.preferredNetwork,
141
- value,
142
- );
143
- });
144
-
145
- it('tokenize (unsettable)', () => {
146
- SetterGetter(
147
- (value: boolean) => (card._data.tokenize = value),
148
- () => card._data.tokenize,
149
- true,
150
- );
151
- card._data.tokenize = null;
152
- SetterGetter(
153
- (value: boolean) => (card._data.tokenize = value),
154
- () => card._data.tokenize,
155
- false,
156
- );
157
- });
158
-
159
- it('zipCode', () => {
160
- SetterGetter(
161
- (value: string) => card.setZipCode(value),
162
- () => card._data.zipCode,
163
- randomString(2, 8),
164
- );
165
- });
166
- });
167
- });
@@ -1,146 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { CardApiEndpoint } from '#card/api-endpoint.js';
4
- import { CardModel, IdCard } from '#card/model.js';
5
- import { CardRepository } from '#card/repository.js';
6
- import { cardValidator } from '#card/validator.js';
7
- import { AbstractRepository } from '#core/abstract-repository.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 = 'card';
15
- const id = `${idPrefix}_${randomString(24)}` as IdCard;
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
- delete() {
29
- return endpointResource;
30
- }
31
- send(id, data: Record<string, unknown>) {
32
- if (id) {
33
- if (id === stancerErrorID) {
34
- throw new StancerError();
35
- }
36
- if (id === stancerRouteInaccessibleId) {
37
- throw new StancerRouteInaccessibleError('PATCH', 'stub');
38
- }
39
- if (id === dataUnmodified) {
40
- return;
41
- }
42
- }
43
- return { ...data, id: dataHasBeenSentId };
44
- }
45
- get() {
46
- return endpointResource;
47
- }
48
- }
49
-
50
- it('can be instanciated', () => {
51
- const instance = new CardRepository();
52
- expect(instance).toBeInstanceOf(CardRepository);
53
- expect(instance).toBeInstanceOf(AbstractRepository);
54
- //accessing private method & property
55
- expect(instance.endpoint).toBeInstanceOf(CardApiEndpoint);
56
- expect(instance.validator).toBe(cardValidator);
57
- expect(instance.staticModel).toBe(CardModel);
58
- });
59
-
60
- describe('repository', () => {
61
- const mockRepo = new MockApiEndpoint() as unknown as CardApiEndpoint; // I lie, for convenience sake
62
- const repository = new CardRepository(mockRepo);
63
-
64
- describe('create', () => {
65
- it("doesn't accept bad data", async () => {
66
- const badID = randomString(30, 56);
67
- // using puposefuly bad ID
68
- await expect(async () => await repository.create(badID)).rejects.toThrow();
69
- });
70
-
71
- it('accept good data', async () => {
72
- const model = await repository.create(id);
73
- expect(model).toBeInstanceOf(CardModel);
74
- });
75
- });
76
-
77
- it('delete', async () => {
78
- const modelWithoutId = await repository.create();
79
- await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
80
-
81
- const model = await repository.create({ id: endpointResource.id, ...creationResource });
82
- await expect((await repository.delete(model))._data).toStrictEqual(endpointResource);
83
- });
84
-
85
- describe('send', () => {
86
- it('should return model', async () => {
87
- const model = await repository.create(creationResource);
88
- const sentModel = await repository.send(model);
89
- expect(sentModel).toStrictEqual(model);
90
- expect(sentModel._data.id).toEqual(dataHasBeenSentId);
91
- });
92
-
93
- it('should throw on inprecise error', async () => {
94
- const model = await repository.create({ ...creationResource, id: stancerErrorID as IdCard });
95
- await expect(() => repository.send(model)).rejects.toThrow(StancerError);
96
- });
97
-
98
- it('should return error on StancerRouteInacessibleError', async () => {
99
- const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdCard });
100
- await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
101
- });
102
-
103
- it('should return unmodified model when no new Data', async () => {
104
- const model = await repository.create({ ...creationResource, id: dataUnmodified as IdCard });
105
- const sentModel = await repository.send(model);
106
- expect(model).toStrictEqual(sentModel);
107
- expect(sentModel._data.id).toEqual(dataUnmodified);
108
- });
109
- });
110
-
111
- describe('populate', () => {
112
- it('should not modify model if no Id', async () => {
113
- const getEndpoint = vi.spyOn(mockRepo, 'get');
114
- const model = await repository.create(creationResource);
115
- expect(model).toBeInstanceOf(CardModel);
116
- expect(getEndpoint).toHaveBeenCalledTimes(0);
117
- });
118
-
119
- it('should get model if only id', async () => {
120
- const getEndpoint = vi.spyOn(mockRepo, 'get');
121
- const model = await repository.create(id);
122
- expect(model).toBeInstanceOf(CardModel);
123
- expect(getEndpoint).toHaveBeenCalledTimes(1);
124
- });
125
- });
126
-
127
- describe('list', async () => {
128
- it('return correct model one by one', async () => {
129
- const listStub = repository.list();
130
- const item1 = await listStub.next();
131
- expect(item1.done).toBeFalsy();
132
- expect(item1.value).toHaveProperty('id');
133
- expect(item1.value).toBeInstanceOf(CardModel);
134
- expect(item1.value.name).toBe('item1');
135
-
136
- const item2 = await listStub.next();
137
- expect(item2.done).toBeFalsy();
138
- expect(item2.value).toHaveProperty('id');
139
- expect(item2.value).toBeInstanceOf(CardModel);
140
- expect(item2.value.name).toBe('item2');
141
-
142
- const item3 = await listStub.next();
143
- expect(item3.done).toBeTruthy();
144
- });
145
- });
146
- });