wabe 0.6.12 → 0.6.14

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 (156) hide show
  1. package/dist/database/DatabaseController.d.ts +2 -0
  2. package/dist/file/FileDevAdapter.d.ts +1 -0
  3. package/dist/graphql/pointerAndRelationFunction.d.ts +6 -0
  4. package/dist/index.js +3827 -3541
  5. package/dist/schema/Schema.d.ts +2 -2
  6. package/dist/server/generateCodegen.d.ts +10 -0
  7. package/dist/server/index.d.ts +2 -1
  8. package/dist/utils/objectKeys.d.ts +1 -0
  9. package/package.json +7 -4
  10. package/dev/index.ts +0 -215
  11. package/dist/schema/resolvers/sendEmail.d.ts +0 -1
  12. package/generated/schema.graphql +0 -1945
  13. package/generated/wabe.ts +0 -448
  14. package/src/authentication/OTP.test.ts +0 -69
  15. package/src/authentication/OTP.ts +0 -64
  16. package/src/authentication/Session.test.ts +0 -629
  17. package/src/authentication/Session.ts +0 -517
  18. package/src/authentication/cookies.ts +0 -10
  19. package/src/authentication/defaultAuthentication.ts +0 -209
  20. package/src/authentication/index.ts +0 -4
  21. package/src/authentication/interface.ts +0 -177
  22. package/src/authentication/oauth/GitHub.test.ts +0 -91
  23. package/src/authentication/oauth/GitHub.ts +0 -121
  24. package/src/authentication/oauth/Google.test.ts +0 -91
  25. package/src/authentication/oauth/Google.ts +0 -101
  26. package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
  27. package/src/authentication/oauth/Oauth2Client.ts +0 -135
  28. package/src/authentication/oauth/index.ts +0 -2
  29. package/src/authentication/oauth/utils.test.ts +0 -33
  30. package/src/authentication/oauth/utils.ts +0 -27
  31. package/src/authentication/providers/EmailOTP.test.ts +0 -127
  32. package/src/authentication/providers/EmailOTP.ts +0 -95
  33. package/src/authentication/providers/EmailPassword.test.ts +0 -263
  34. package/src/authentication/providers/EmailPassword.ts +0 -138
  35. package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
  36. package/src/authentication/providers/EmailPasswordSRP.ts +0 -191
  37. package/src/authentication/providers/GitHub.ts +0 -24
  38. package/src/authentication/providers/Google.ts +0 -24
  39. package/src/authentication/providers/OAuth.test.ts +0 -185
  40. package/src/authentication/providers/OAuth.ts +0 -106
  41. package/src/authentication/providers/PhonePassword.test.ts +0 -221
  42. package/src/authentication/providers/PhonePassword.ts +0 -136
  43. package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
  44. package/src/authentication/providers/QRCodeOTP.ts +0 -69
  45. package/src/authentication/providers/index.ts +0 -6
  46. package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
  47. package/src/authentication/resolvers/refreshResolver.ts +0 -19
  48. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
  49. package/src/authentication/resolvers/signInWithResolver.test.ts +0 -306
  50. package/src/authentication/resolvers/signInWithResolver.ts +0 -106
  51. package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
  52. package/src/authentication/resolvers/signOutResolver.ts +0 -18
  53. package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
  54. package/src/authentication/resolvers/signUpWithResolver.ts +0 -68
  55. package/src/authentication/resolvers/verifyChallenge.test.ts +0 -230
  56. package/src/authentication/resolvers/verifyChallenge.ts +0 -78
  57. package/src/authentication/roles.test.ts +0 -49
  58. package/src/authentication/roles.ts +0 -40
  59. package/src/authentication/security.ts +0 -278
  60. package/src/authentication/utils.test.ts +0 -97
  61. package/src/authentication/utils.ts +0 -39
  62. package/src/cache/InMemoryCache.test.ts +0 -62
  63. package/src/cache/InMemoryCache.ts +0 -45
  64. package/src/cron/index.test.ts +0 -17
  65. package/src/cron/index.ts +0 -43
  66. package/src/database/DatabaseController.test.ts +0 -613
  67. package/src/database/DatabaseController.ts +0 -1415
  68. package/src/database/index.test.ts +0 -1551
  69. package/src/database/index.ts +0 -9
  70. package/src/database/interface.ts +0 -308
  71. package/src/email/DevAdapter.ts +0 -7
  72. package/src/email/EmailController.test.ts +0 -29
  73. package/src/email/EmailController.ts +0 -13
  74. package/src/email/index.ts +0 -2
  75. package/src/email/interface.ts +0 -36
  76. package/src/email/templates/sendOtpCode.ts +0 -120
  77. package/src/file/FileController.ts +0 -28
  78. package/src/file/FileDevAdapter.ts +0 -51
  79. package/src/file/hookDeleteFile.ts +0 -25
  80. package/src/file/hookReadFile.ts +0 -66
  81. package/src/file/hookUploadFile.ts +0 -52
  82. package/src/file/index.test.ts +0 -1031
  83. package/src/file/index.ts +0 -2
  84. package/src/file/interface.ts +0 -63
  85. package/src/file/security.ts +0 -156
  86. package/src/graphql/GraphQLSchema.test.ts +0 -5099
  87. package/src/graphql/GraphQLSchema.ts +0 -886
  88. package/src/graphql/index.ts +0 -2
  89. package/src/graphql/parseGraphqlSchema.ts +0 -85
  90. package/src/graphql/parser.test.ts +0 -203
  91. package/src/graphql/parser.ts +0 -707
  92. package/src/graphql/pointerAndRelationFunction.ts +0 -191
  93. package/src/graphql/resolvers.ts +0 -464
  94. package/src/graphql/tests/aggregation.test.ts +0 -1115
  95. package/src/graphql/tests/e2e.test.ts +0 -590
  96. package/src/graphql/tests/scalars.test.ts +0 -250
  97. package/src/graphql/types.ts +0 -227
  98. package/src/hooks/HookObject.test.ts +0 -122
  99. package/src/hooks/HookObject.ts +0 -165
  100. package/src/hooks/authentication.ts +0 -67
  101. package/src/hooks/createUser.test.ts +0 -77
  102. package/src/hooks/createUser.ts +0 -10
  103. package/src/hooks/defaultFields.test.ts +0 -176
  104. package/src/hooks/defaultFields.ts +0 -32
  105. package/src/hooks/deleteSession.test.ts +0 -181
  106. package/src/hooks/deleteSession.ts +0 -20
  107. package/src/hooks/hashFieldHook.test.ts +0 -152
  108. package/src/hooks/hashFieldHook.ts +0 -89
  109. package/src/hooks/index.test.ts +0 -258
  110. package/src/hooks/index.ts +0 -420
  111. package/src/hooks/permissions.test.ts +0 -412
  112. package/src/hooks/permissions.ts +0 -93
  113. package/src/hooks/protected.test.ts +0 -551
  114. package/src/hooks/protected.ts +0 -74
  115. package/src/hooks/searchableFields.test.ts +0 -147
  116. package/src/hooks/searchableFields.ts +0 -86
  117. package/src/hooks/session.test.ts +0 -134
  118. package/src/hooks/session.ts +0 -76
  119. package/src/hooks/setEmail.test.ts +0 -216
  120. package/src/hooks/setEmail.ts +0 -33
  121. package/src/hooks/setupAcl.test.ts +0 -618
  122. package/src/hooks/setupAcl.ts +0 -25
  123. package/src/hooks/virtualFields.test.ts +0 -228
  124. package/src/hooks/virtualFields.ts +0 -48
  125. package/src/index.ts +0 -9
  126. package/src/schema/Schema.test.ts +0 -482
  127. package/src/schema/Schema.ts +0 -839
  128. package/src/schema/defaultResolvers.ts +0 -93
  129. package/src/schema/index.ts +0 -1
  130. package/src/schema/resolvers/meResolver.test.ts +0 -62
  131. package/src/schema/resolvers/meResolver.ts +0 -10
  132. package/src/schema/resolvers/resetPassword.test.ts +0 -341
  133. package/src/schema/resolvers/resetPassword.ts +0 -63
  134. package/src/schema/resolvers/sendEmail.test.ts +0 -118
  135. package/src/schema/resolvers/sendEmail.ts +0 -21
  136. package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
  137. package/src/schema/resolvers/sendOtpCode.ts +0 -52
  138. package/src/security.test.ts +0 -4136
  139. package/src/server/defaultSessionHandler.test.ts +0 -62
  140. package/src/server/defaultSessionHandler.ts +0 -104
  141. package/src/server/generateCodegen.ts +0 -433
  142. package/src/server/index.test.ts +0 -843
  143. package/src/server/index.ts +0 -336
  144. package/src/server/interface.ts +0 -11
  145. package/src/server/routes/authHandler.ts +0 -171
  146. package/src/server/routes/index.ts +0 -48
  147. package/src/utils/crypto.test.ts +0 -41
  148. package/src/utils/crypto.ts +0 -105
  149. package/src/utils/database.ts +0 -8
  150. package/src/utils/export.ts +0 -12
  151. package/src/utils/helper.ts +0 -204
  152. package/src/utils/index.test.ts +0 -11
  153. package/src/utils/index.ts +0 -196
  154. package/src/utils/preload.ts +0 -8
  155. package/src/utils/testHelper.ts +0 -124
  156. package/tsconfig.json +0 -32
@@ -1,250 +0,0 @@
1
- import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
2
- import type { Wabe } from '../../server'
3
- import { type GraphQLClient, gql } from 'graphql-request'
4
- import { type DevWabeTypes, getGraphqlClient } from '../../utils/helper'
5
- import { setupTests, closeTests } from '../../utils/testHelper'
6
-
7
- describe('GraphQL : Scalars', () => {
8
- let wabe: Wabe<DevWabeTypes>
9
- let port: number
10
- let client: GraphQLClient
11
-
12
- const now = new Date()
13
-
14
- beforeAll(async () => {
15
- const setup = await setupTests()
16
- wabe = setup.wabe
17
- port = setup.port
18
- client = getGraphqlClient(port)
19
- })
20
-
21
- afterAll(async () => {
22
- await closeTests(wabe)
23
- })
24
-
25
- describe('Date', () => {
26
- it('should create a date with JavaScript Date', async () => {
27
- const { createUsers } = await client.request<any>(graphql.createUsers, {
28
- input: {
29
- fields: [
30
- {
31
- name: 'Jean',
32
- birthDate: now,
33
- email: 'email@test.fr',
34
- },
35
- ],
36
- },
37
- })
38
-
39
- expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
40
- })
41
-
42
- it('should create a date with timestamp in number', async () => {
43
- const { createUsers } = await client.request<any>(graphql.createUsers, {
44
- input: {
45
- fields: [
46
- {
47
- name: 'Jean2',
48
- birthDate: now.getTime(),
49
- email: 'email@test.fr',
50
- },
51
- ],
52
- },
53
- })
54
-
55
- expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
56
- })
57
-
58
- it('should create a date with iso string', async () => {
59
- const { createUsers } = await client.request<any>(graphql.createUsers, {
60
- input: {
61
- fields: [
62
- {
63
- name: 'Jean3',
64
- birthDate: now.toISOString(),
65
- email: 'email@test.fr',
66
- },
67
- ],
68
- },
69
- })
70
-
71
- expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
72
- })
73
-
74
- it('should create a date with partial date', async () => {
75
- const { createUsers } = await client.request<any>(graphql.createUsers, {
76
- input: {
77
- fields: [
78
- {
79
- name: 'Jean4',
80
- birthDate: '2023-12-20',
81
- email: 'email@test.fr',
82
- },
83
- ],
84
- },
85
- })
86
-
87
- const birthDate = new Date(createUsers.edges[0].node.birthDate)
88
- const date = new Date('2023-12-20')
89
-
90
- expect(date.getFullYear()).toEqual(birthDate.getFullYear())
91
- expect(date.getMonth()).toEqual(birthDate.getMonth())
92
- expect(date.getDate()).toEqual(birthDate.getDate())
93
- })
94
-
95
- it('should not create a date with invalid string', () => {
96
- expect(
97
- client.request(graphql.createUsers, {
98
- input: {
99
- fields: [
100
- {
101
- name: 'Jean5',
102
- birthDate: now.getTime().toString(),
103
- },
104
- ],
105
- },
106
- }),
107
- ).rejects.toThrow('Invalid date')
108
-
109
- expect(
110
- client.request(graphql.createUsers, {
111
- input: {
112
- fields: [
113
- {
114
- name: 'Jean5',
115
- birthDate: 'tata',
116
- },
117
- ],
118
- },
119
- }),
120
- ).rejects.toThrow('Invalid date')
121
- })
122
- })
123
-
124
- describe('Email', () => {
125
- it('should create an email', async () => {
126
- const { createUsers } = await client.request<any>(graphql.createUsers, {
127
- input: {
128
- fields: [
129
- {
130
- name: 'Jean',
131
- email: 'jean.doe@gmail.com',
132
- },
133
- ],
134
- },
135
- })
136
-
137
- expect(createUsers.edges[0].node.email).toEqual('jean.doe@gmail.com')
138
-
139
- expect(
140
- client.request(graphql.createUsers, {
141
- input: {
142
- fields: [
143
- {
144
- name: 'Jean',
145
- email: 'oe@gmail.com',
146
- },
147
- ],
148
- },
149
- }),
150
- ).resolves.toEqual(expect.anything())
151
-
152
- expect(
153
- client.request(graphql.createUsers, {
154
- input: {
155
- fields: [
156
- {
157
- name: 'Jean',
158
- email: 'test.@gmail.fr',
159
- },
160
- ],
161
- },
162
- }),
163
- ).resolves.toEqual(expect.anything())
164
- })
165
-
166
- it('should not create an invalid email', () => {
167
- expect(
168
- client.request(graphql.createUsers, {
169
- input: {
170
- fields: [
171
- {
172
- name: 'Jean',
173
- email: 'jean.doe',
174
- },
175
- ],
176
- },
177
- }),
178
- ).rejects.toThrow('Invalid email')
179
-
180
- expect(
181
- client.request(graphql.createUsers, {
182
- input: {
183
- fields: [
184
- {
185
- name: 'Jean',
186
- email: 'jean.doe@gmail',
187
- },
188
- ],
189
- },
190
- }),
191
- ).rejects.toThrow('Invalid email')
192
-
193
- expect(
194
- client.request(graphql.createUsers, {
195
- input: {
196
- fields: [
197
- {
198
- name: 'Jean',
199
- email: '@gmail.com',
200
- },
201
- ],
202
- },
203
- }),
204
- ).rejects.toThrow('Invalid email')
205
-
206
- expect(
207
- client.request(graphql.createUsers, {
208
- input: {
209
- fields: [
210
- {
211
- name: 'Jean',
212
- email: '@gmail',
213
- },
214
- ],
215
- },
216
- }),
217
- ).rejects.toThrow('Invalid email')
218
- })
219
- })
220
- })
221
-
222
- const graphql = {
223
- users: gql`
224
- query users($where: UserWhereInput) {
225
- users(where: $where) {
226
- edges {
227
- node {
228
- id
229
- name
230
- birthDate
231
- email
232
- }
233
- }
234
- }
235
- }
236
- `,
237
- createUsers: gql`
238
- mutation createUsers($input: CreateUsersInput!) {
239
- createUsers(input: $input) {
240
- edges {
241
- node {
242
- name
243
- birthDate
244
- email
245
- }
246
- }
247
- }
248
- }
249
- `,
250
- }
@@ -1,227 +0,0 @@
1
- import {
2
- GraphQLBoolean,
3
- GraphQLFloat,
4
- GraphQLInputObjectType,
5
- GraphQLInt,
6
- GraphQLList,
7
- GraphQLScalarType,
8
- GraphQLString,
9
- GraphQLID,
10
- } from 'graphql'
11
- import { isValidPhoneNumber } from 'libphonenumber-js'
12
- import { tokenize } from '../utils'
13
-
14
- export const AnyScalarType = new GraphQLScalarType({
15
- name: 'Any',
16
- description:
17
- 'The Any scalar type is used in operations and types that involve any type of value.',
18
- parseValue: (value) => value,
19
- serialize: (value) => value,
20
- })
21
-
22
- export const DateScalarType = new GraphQLScalarType({
23
- name: 'Date',
24
- description: 'Date scalar type',
25
- parseValue(value: any): Date {
26
- const date = new Date(value)
27
-
28
- if (Number.isNaN(date.getTime())) throw new Error('Invalid date')
29
-
30
- return date
31
- },
32
- serialize(value: any) {
33
- if (value instanceof Date) return value.toISOString()
34
-
35
- return new Date(value).toISOString()
36
- },
37
- })
38
-
39
- export const EmailScalarType = new GraphQLScalarType({
40
- name: 'Email',
41
- description: 'Email scalar type',
42
- parseValue(value: any): string {
43
- if (typeof value !== 'string') throw new Error('Invalid email')
44
-
45
- if (!value.match(/^[^@\s]+@[^@\s]+\.[^@\s]+$/)) throw new Error('Invalid email')
46
-
47
- return value
48
- },
49
- })
50
-
51
- export const PhoneScalarType = new GraphQLScalarType({
52
- name: 'Phone',
53
- description: 'Phone scalar type',
54
- parseValue(value: any): string {
55
- if (typeof value !== 'string') throw new Error('Invalid phone')
56
-
57
- if (!isValidPhoneNumber(value)) throw new Error('Invalid phone number')
58
-
59
- return value
60
- },
61
- })
62
-
63
- const parseFileValue = (value: any) => {
64
- if (value instanceof Blob) return value
65
-
66
- throw new Error('Invalid file')
67
- }
68
-
69
- export const FileScalarType = new GraphQLScalarType({
70
- name: 'File',
71
- description: 'File scalar type',
72
- parseValue: parseFileValue,
73
- serialize: (value: any) => {
74
- return value
75
- },
76
- parseLiteral: () => {
77
- throw new Error('Invalid file')
78
- },
79
- })
80
-
81
- export const SearchScalarType = new GraphQLScalarType({
82
- name: 'Search',
83
- description: 'Search scalar to tokenize and search for all searchable fields',
84
- parseValue: (value: any) => {
85
- if (typeof value !== 'string') throw new Error('Invalid search term')
86
-
87
- if (value === '') return ''
88
-
89
- return tokenize(value).split(' ')
90
- },
91
- })
92
-
93
- export const SearchWhereInput = new GraphQLInputObjectType({
94
- name: 'SearchWhereInput',
95
- fields: {
96
- contains: { type: SearchScalarType },
97
- },
98
- })
99
-
100
- export const AnyWhereInput = new GraphQLInputObjectType({
101
- name: 'AnyWhereInput',
102
- fields: {
103
- equalTo: { type: AnyScalarType },
104
- notEqualTo: { type: AnyScalarType },
105
- exists: { type: GraphQLBoolean },
106
- },
107
- })
108
-
109
- export const FileWhereInput = new GraphQLInputObjectType({
110
- name: 'FileWhereInput',
111
- fields: {
112
- equalTo: { type: FileScalarType },
113
- notEqualTo: { type: FileScalarType },
114
- in: { type: new GraphQLList(FileScalarType) },
115
- notInt: { type: new GraphQLList(FileScalarType) },
116
- },
117
- })
118
-
119
- export const ArrayWhereInput = new GraphQLInputObjectType({
120
- name: 'ArrayWhereInput',
121
- fields: {
122
- equalTo: { type: AnyScalarType },
123
- notEqualTo: { type: AnyScalarType },
124
- contains: { type: AnyScalarType },
125
- notContains: { type: AnyScalarType },
126
- exists: { type: GraphQLBoolean },
127
- },
128
- })
129
-
130
- export const DateWhereInput = new GraphQLInputObjectType({
131
- name: 'DateWhereInput',
132
- fields: {
133
- equalTo: { type: DateScalarType },
134
- notEqualTo: { type: DateScalarType },
135
- in: { type: new GraphQLList(DateScalarType) },
136
- notIn: { type: new GraphQLList(DateScalarType) },
137
- lessThan: { type: DateScalarType },
138
- lessThanOrEqualTo: { type: DateScalarType },
139
- greaterThan: { type: DateScalarType },
140
- greaterThanOrEqualTo: { type: DateScalarType },
141
- exists: { type: GraphQLBoolean },
142
- },
143
- })
144
-
145
- export const EmailWhereInput = new GraphQLInputObjectType({
146
- name: 'EmailWhereInput',
147
- fields: {
148
- equalTo: { type: EmailScalarType },
149
- notEqualTo: { type: EmailScalarType },
150
- in: { type: new GraphQLList(EmailScalarType) },
151
- notIn: { type: new GraphQLList(EmailScalarType) },
152
- exists: { type: GraphQLBoolean },
153
- },
154
- })
155
-
156
- export const PhoneWhereInput = new GraphQLInputObjectType({
157
- name: 'PhoneWhereInput',
158
- fields: {
159
- equalTo: { type: PhoneScalarType },
160
- notEqualTo: { type: PhoneScalarType },
161
- in: { type: new GraphQLList(PhoneScalarType) },
162
- notIn: { type: new GraphQLList(PhoneScalarType) },
163
- exists: { type: GraphQLBoolean },
164
- },
165
- })
166
-
167
- export const IdWhereInput = new GraphQLInputObjectType({
168
- name: 'IdWhereInput',
169
- fields: {
170
- equalTo: { type: GraphQLID },
171
- notEqualTo: { type: GraphQLID },
172
- in: { type: new GraphQLList(GraphQLID) },
173
- notIn: { type: new GraphQLList(GraphQLID) },
174
- },
175
- })
176
-
177
- export const StringWhereInput = new GraphQLInputObjectType({
178
- name: 'StringWhereInput',
179
- fields: {
180
- equalTo: { type: GraphQLString },
181
- notEqualTo: { type: GraphQLString },
182
- in: { type: new GraphQLList(GraphQLString) },
183
- notIn: { type: new GraphQLList(GraphQLString) },
184
- exists: { type: GraphQLBoolean },
185
- },
186
- })
187
-
188
- export const IntWhereInput = new GraphQLInputObjectType({
189
- name: 'IntWhereInput',
190
- fields: {
191
- equalTo: { type: GraphQLInt },
192
- notEqualTo: { type: GraphQLInt },
193
- lessThan: { type: GraphQLInt },
194
- lessThanOrEqualTo: { type: GraphQLInt },
195
- greaterThan: { type: GraphQLInt },
196
- greaterThanOrEqualTo: { type: GraphQLInt },
197
- in: { type: new GraphQLList(GraphQLInt) },
198
- notIn: { type: new GraphQLList(GraphQLInt) },
199
- exists: { type: GraphQLBoolean },
200
- },
201
- })
202
-
203
- export const FloatWhereInput = new GraphQLInputObjectType({
204
- name: 'FloatWhereInput',
205
- fields: {
206
- equalTo: { type: GraphQLFloat },
207
- notEqualTo: { type: GraphQLFloat },
208
- lessThan: { type: GraphQLFloat },
209
- lessThanOrEqualTo: { type: GraphQLFloat },
210
- greaterThan: { type: GraphQLFloat },
211
- greaterThanOrEqualTo: { type: GraphQLFloat },
212
- in: { type: new GraphQLList(GraphQLFloat) },
213
- notIn: { type: new GraphQLList(GraphQLFloat) },
214
- exists: { type: GraphQLBoolean },
215
- },
216
- })
217
-
218
- export const BooleanWhereInput = new GraphQLInputObjectType({
219
- name: 'BooleanWhereInput',
220
- fields: {
221
- equalTo: { type: GraphQLBoolean },
222
- notEqualTo: { type: GraphQLBoolean },
223
- in: { type: new GraphQLList(GraphQLBoolean) },
224
- notIn: { type: new GraphQLList(GraphQLBoolean) },
225
- exists: { type: GraphQLBoolean },
226
- },
227
- })
@@ -1,122 +0,0 @@
1
- import { describe, expect, it, beforeAll, afterAll } from 'bun:test'
2
- import { HookObject } from './HookObject'
3
- import { OperationType } from '.'
4
- import type { DevWabeTypes } from '../utils/helper'
5
- import { setupTests, closeTests } from '../utils/testHelper'
6
- import type { Wabe } from '../server'
7
-
8
- describe('HookObject', () => {
9
- let wabe: Wabe<DevWabeTypes>
10
-
11
- beforeAll(async () => {
12
- const setup = await setupTests()
13
- wabe = setup.wabe
14
- })
15
-
16
- afterAll(async () => {
17
- await closeTests(wabe)
18
- })
19
-
20
- it('should fetch all the fields of an object', async () => {
21
- const res = await wabe.controllers.database.createObject({
22
- className: 'User',
23
- data: { age: 30, name: 'John Doe' },
24
- context: {
25
- wabe,
26
- isRoot: true,
27
- },
28
- select: { id: true },
29
- })
30
-
31
- const hookObject = new HookObject<DevWabeTypes, 'User'>({
32
- className: 'User',
33
- // @ts-expect-error
34
- newData: { age: 30, name: 'John Doe' },
35
- context: {
36
- wabe,
37
- } as any,
38
- operationType: OperationType.BeforeCreate,
39
- object: {
40
- id: res?.id || 'id',
41
- },
42
- select: {},
43
- })
44
-
45
- const fetchResult = await hookObject.fetch()
46
-
47
- expect(fetchResult).toEqual(
48
- expect.objectContaining({
49
- id: res?.id || 'id',
50
- age: 30,
51
- name: 'John Doe',
52
- }),
53
- )
54
- })
55
-
56
- it('should return correctly value depends on the update state of the field', () => {
57
- const userData = { name: 'John Doe' }
58
-
59
- const hookObject = new HookObject<DevWabeTypes, 'User'>({
60
- className: 'User',
61
- // @ts-expect-error
62
- newData: userData,
63
- context: {} as any,
64
- operationType: OperationType.BeforeUpdate,
65
- object: {
66
- id: '1',
67
- },
68
- select: {},
69
- })
70
-
71
- expect(hookObject.isFieldUpdated('name')).toBeTrue()
72
- expect(hookObject.isFieldUpdated('age')).toBeFalse()
73
- })
74
-
75
- it('should create a clone of the data', () => {
76
- const userData = { name: 'John Doe', age: 30 }
77
-
78
- const hookObject = new HookObject<DevWabeTypes, 'User'>({
79
- className: 'User',
80
- newData: userData as any,
81
- operationType: OperationType.BeforeCreate,
82
- context: {} as any,
83
- object: {
84
- id: '1',
85
- },
86
- select: {},
87
- })
88
-
89
- hookObject.upsertNewData('name', 'tata')
90
-
91
- expect(hookObject.getNewData()).toEqual(
92
- expect.objectContaining({
93
- name: 'tata',
94
- age: 30,
95
- }),
96
- )
97
- })
98
-
99
- it('should not set data for an after hook', () => {
100
- const userData = { name: 'John Doe', age: 30 }
101
-
102
- const hookObject = new HookObject({
103
- className: 'User',
104
- newData: userData as any,
105
- operationType: OperationType.AfterCreate,
106
- context: {} as any,
107
- object: {
108
- id: '1',
109
- },
110
- select: {},
111
- })
112
-
113
- expect(() => hookObject.upsertNewData('name', 'tata')).toThrow(
114
- 'Cannot set data in a hook that is not a before hook',
115
- )
116
-
117
- expect(hookObject.getNewData()).toEqual({
118
- name: 'John Doe',
119
- age: 30,
120
- })
121
- })
122
- })