UNPKG

5.13 kBJavaScriptView Raw
1import {
2 GQLBase,
3 META_KEY,
4 Properties,
5 Schema,
6 resolver,
7 mutator,
8 typeOf
9} from '../es6/lattice'
10
11describe('Test getProps for testing various prop fields', async () => {
12 @Schema(/* GraphQL */`
13 type Contrived {
14 name: String
15 age: Int
16 job: String
17 location: String
18 }
19
20 type Query {
21 contrivances: [Contrived]
22 contrived(id: ID): Contrived
23 }
24 `)
25 // Check a model property value with a different name
26 @Properties(['location', 'loc'])
27 class Contrived extends GQLBase {
28 // Synchronous function property
29 name() { return 'Jane' }
30
31 // Asynchronous function property
32 async age() {
33 let deferred = {}
34
35 deferred.promise = new Promise((resolve, reject) => {
36 deferred.resolve = resolve
37 deferred.reject = reject
38 })
39
40 setTimeout(() => {
41 deferred.resolve(21)
42 }, 500)
43
44 return deferred.promise
45 }
46
47 // Getter function property
48 get job() {
49 return 'Saleswoman'
50 }
51
52 @resolver contrived(requestData, {id}) {
53 return new Contrived({loc: id})
54 }
55
56 static async RESOLVERS(requestData) {
57 return {
58 contrivances() {
59 return [new Contrived({loc: 'Mars'})]
60 }
61 }
62 }
63 }
64
65 it('should allow the fetching of all props via getProps', async () => {
66 let instance = new Contrived({ loc: 'SF'})
67 let name = await instance.callProp('name')
68 let age = await instance.callProp('age')
69 let job = await instance.callProp('job')
70 let location = await instance.callProp('location')
71
72 expect(name).toBe('Jane')
73 expect(age).toBe(21)
74 expect(job).toBe('Saleswoman')
75 expect(location).toBe('SF')
76 })
77
78 it('should support resolvers from both RESOLVERS and @resolver', async () => {
79 let instance = new Contrived({loc: 'Japan'})
80 let root = await Contrived.getMergedRoot({req: true, res: true})
81
82 expect(typeof root.contrived).toBe('function')
83 expect(typeof root.contrivances).toBe('function')
84 })
85})
86
87describe('Check getResolver usage in instance and static forms', async () => {
88 const Red = 'Red'
89 const Green = 'Green'
90 const Amber = 'Amber'
91 const Violet = 'Violet'
92
93 const stockModel = {
94 pwrColor: Red,
95 hddColor: Green,
96 fddColor: Amber
97 };
98
99 @Schema(/* GraphQL */`
100 type Bifrost {
101 pwrColor: String
102 hddColor: String
103 fddColor: String
104 }
105
106 type Query {
107 stockBifrost: Bifrost
108 }
109
110 type Mutation {
111 createBifrost(
112 pwrColor: String
113 hddColor: String
114 fddColor: String
115 ): Bifrost
116 }
117 `)
118 class Bifrost extends GQLBase {
119 async RESOLVERS(requestData) {
120 return {
121 async stockBifrost(requestData) {
122 return new Bifrost(stockModel)
123 }
124 }
125 }
126
127 @mutator
128 async createBifrost(requestData, modelData) {
129 if (requestData || requestData.pwrColor) {
130 modelData.pwrColor = requestData.pwrColor
131 }
132
133 return new Bifrost(modelData, requestData)
134 }
135 }
136
137 it('should work with instances', async () => {
138 let reqData = { pwrColor: Violet }
139 let instance = new Bifrost(stockModel, reqData)
140 let createBifrost = await instance.getResolver('createBifrost')
141 let _inst = await createBifrost(stockModel)
142
143 // Ensure the function was received from getResolver
144 expect(createBifrost).not.toBeNull()
145
146 // Ensure it is of type function as expected
147 expect(typeOf(createBifrost)).toBe('Function')
148
149 // Ensure that the results of calling the function are not null
150 expect(_inst).not.toBeNull()
151
152 // Ensure that the requestData.pwrColor value was used as per our
153 // mutator's code. (Contrived but easy way to test the request
154 // data was bound in the call to getResolver())
155 expect(await _inst.callProp('pwrColor')).toBe(Violet)
156 })
157
158 it('should work with the class as a static function', async () => {
159 let reqData = { pwrColor: Violet }
160 let createBifrost = await Bifrost.getResolver('createBifrost', reqData)
161 let _inst = await createBifrost(stockModel)
162
163 // Ensure the function was received from getResolver
164 expect(createBifrost).not.toBeNull()
165
166 // Ensure it is of type function as expected
167 expect(typeOf(createBifrost)).toBe('Function')
168
169 // Ensure that the results of calling the function are not null
170 expect(_inst).not.toBeNull()
171
172 // Ensure that the requestData.pwrColor value was used as per our
173 // mutator's code. (Contrived but easy way to test the request
174 // data was bound in the call to getResolver())
175 expect(await _inst.callProp('pwrColor')).toBe(Violet)
176 })
177
178})
179
180describe('Check for expected thrown errors in GQLBase', async () => {
181 it('should fail if the class has no schema', async() => {
182 class ContrivedType extends GQLBase { }
183
184 expect(() => {
185 const type = new ContrivedType()
186 }).toThrow()
187 })
188
189 it('should fail if the class name does not match a type', async() => {
190 @Schema('type VeryContrivedType { name: String }')
191 class ContrivedType extends GQLBase { }
192
193 expect(() => {
194 const type = new ContrivedType()
195 }).toThrow()
196 })
197})
\No newline at end of file