1 | import {
|
2 | GQLBase,
|
3 | META_KEY,
|
4 | Properties,
|
5 | Schema,
|
6 | resolver,
|
7 | mutator,
|
8 | typeOf
|
9 | } from '../es6/lattice'
|
10 |
|
11 | describe('Test getProps for testing various prop fields', async () => {
|
12 | @Schema(`
|
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 |
|
26 | @Properties(['location', 'loc'])
|
27 | class Contrived extends GQLBase {
|
28 |
|
29 | name() { return 'Jane' }
|
30 |
|
31 |
|
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 |
|
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 |
|
87 | describe('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(`
|
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 |
|
144 | expect(createBifrost).not.toBeNull()
|
145 |
|
146 |
|
147 | expect(typeOf(createBifrost)).toBe('Function')
|
148 |
|
149 |
|
150 | expect(_inst).not.toBeNull()
|
151 |
|
152 |
|
153 |
|
154 |
|
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 |
|
164 | expect(createBifrost).not.toBeNull()
|
165 |
|
166 |
|
167 | expect(typeOf(createBifrost)).toBe('Function')
|
168 |
|
169 |
|
170 | expect(_inst).not.toBeNull()
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | expect(await _inst.callProp('pwrColor')).toBe(Violet)
|
176 | })
|
177 |
|
178 | })
|
179 |
|
180 | describe('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 |