Skip to content

Commit 16aec5f

Browse files
committed
created new test file for graphQLControllerTests, implemented test for openGraphQLConnection function
1 parent a57be1a commit 16aec5f

File tree

4 files changed

+165
-34
lines changed

4 files changed

+165
-34
lines changed

src/client/controllers/graphQLController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const graphQLController: GqlController = {
7171
.catch((err) => console.log('error in sendGqlToMain', err));
7272
},
7373

74+
7475
openGraphQLConnectionAndRunCollection(reqResArray: ReqRes[]): void {
7576
// initialize response data
7677
let index = 0;
@@ -102,7 +103,7 @@ const graphQLController: GqlController = {
102103
});
103104

104105
const runSingleGraphQLRequest = (reqResObj: ReqRes) => {
105-
reqResObj.response.headers = {};
106+
reqResObj.response.headers = {};
106107
reqResObj.response.events = [];
107108
reqResObj.response.cookies = [];
108109
reqResObj.connection = 'open';

src/client/controllers/reqResController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const connectionController = {
8989
}
9090
},
9191

92+
/* this functionality seems to be broken - Brooke */
9293
runCollectionTest(reqResArray: ReqRes[]): void {
9394
api.removeAllListeners('reqResUpdate');
9495
let index = 0;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import graphQLController from '../../src/client/controllers/graphQLController.ts';
2+
3+
/*I went ahead and restructured the graphQLControllerTest file so that it mirrors the order in which the functions
4+
appear in the graphQLController file -Brooke*/
5+
describe('graphQLController', () => {
6+
7+
/* the openGraphQLConnection function includes calls to sendGqlMain and handleRespnose, I created mocks for those functions
8+
to fully isolate openGraphQL functionality */
9+
describe('openGraphQLConnection', () => {
10+
11+
beforeAll(() => {
12+
graphQLController.sendGqlToMain = jest.fn().mockResolvedValue({}); // utilized mockResolvedValue since sendGqlToMain returns a promise
13+
graphQLController.handleResponse = jest.fn();
14+
})
15+
16+
afterEach(() => {
17+
graphQLController.sendGqlToMain.mockClear();
18+
graphQLController.handleResponse.mockClear();
19+
})
20+
21+
it('should initialize response data correctly', async () => {
22+
const reqResObj = {};
23+
await graphQLController.openGraphQLConnection(reqResObj);
24+
25+
/* each of these assertions checks the properties on the reqResObj to make sure they were updated correctly
26+
before being passed into the mock sendGqltoMain function */
27+
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.headers).toEqual({});
28+
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.events).toEqual([]);
29+
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.cookies).toEqual([]);
30+
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.connection).toEqual('open');
31+
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.timeSent).toBeGreaterThan(0);
32+
});
33+
34+
// it ('should call sendGqlToMain' , async () => {
35+
// const sendGqlToMainSpy = jest.spyOn(graphQLController, 'sendGqlToMain');
36+
// await graphQLController.openGraphQLConnection(reqResObj);
37+
// // expect(sendGqlToMainSpy.response).toHaveBeenCalledWith({ reqResObj }.response);
38+
// expect(sendGqlToMainSpy).toBeCalledTimes(1);
39+
// });
40+
41+
// })
42+
})
43+
44+
45+
// describe('sendGqlToMain', () => {
46+
47+
// })
48+
49+
// describe('openSubscription', () => {
50+
51+
// })
52+
53+
// describe('closeSubscription', () => {
54+
55+
// })
56+
// describe('handleResponse', () => {
57+
58+
// })
59+
60+
// describe('handleError', () => {
61+
62+
// })
63+
64+
// describe('cookieFormatter', () => {
65+
66+
// it('should format cookie array', () => {
67+
68+
// // hardcode the cookie array
69+
// const cookieArray = [
70+
// {
71+
// domain: 'localhost',
72+
// expires: '2024-06-12T18:19:03.262Z',
73+
// hostOnly: true,
74+
// httpOnly: false,
75+
// name: 'cookie1',
76+
// path: '/',
77+
// secure: false,
78+
// session: false,
79+
// value: 'value1',
80+
// },
81+
// {
82+
// domain: 'localhost',
83+
// expires: '2024-06-12T18:19:03.262Z',
84+
// hostOnly: true,
85+
// httpOnly: false,
86+
// name: 'cookie2',
87+
// path: '/',
88+
// secure: false,
89+
// session: false,
90+
// value: 'value2',
91+
// },
92+
// ];
93+
94+
// // and the expected output
95+
// const expectedOutput = [
96+
// {
97+
// domain: 'localhost',
98+
// expires: '2024-06-12T18:19:03.262Z',
99+
// hostOnly: true,
100+
// httpOnly: false,
101+
// name: 'cookie1',
102+
// path: '/',
103+
// secure: false,
104+
// session: false,
105+
// value: 'value1',
106+
// },
107+
// {
108+
// domain: 'localhost',
109+
// expires: '2024-06-12T18:19:03.262Z',
110+
// hostOnly: true,
111+
// httpOnly: false,
112+
// name: 'cookie2',
113+
// path: '/',
114+
// secure: false,
115+
// session: false,
116+
// value: 'value2',
117+
// },
118+
// ];
119+
120+
// expect(graphQLController.cookieFormatter(cookieArray)).toEqual(
121+
// expectedOutput
122+
// );
123+
// });
124+
125+
// })

test/__tests__/graphQLcontrollerTest.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ describe('graphQLController', () => {
7272
jest.resetAllMocks();
7373
});
7474

75-
xit('should call api.send and api.receive with the correct arguments', () => {
75+
// this test was previously commented out and fails
76+
it('should call api.send and api.receive with the correct arguments', () => {
7677
const url = 'https://example.com/graphql';
7778
const headers = [{ key: 'Content-Type', value: 'application/json' }];
7879
const cookies = [{ key: 'session_id', value: '1234' }];
@@ -123,7 +124,8 @@ describe('graphQLController', () => {
123124
);
124125
});
125126

126-
xit('should dispatch an action with the error message if the introspection query returns an error', () => {
127+
// this test was previously commented out and fails
128+
it('should dispatch an action with the error message if the introspection query returns an error', () => {
127129
const url = 'https://example.com/graphql';
128130
const headers = [{ key: 'Content-Type', value: 'application/json' }];
129131
const cookies = [{ key: 'session_id', value: '1234' }];
@@ -154,27 +156,28 @@ describe('graphQLController', () => {
154156
);
155157
});
156158

157-
// it('should send introspection query and dispatch introspectionDataChanged action', () => {
158-
// const url = 'http://localhost:4000/graphql';
159-
// const headers = [{ key: 'Authorization', value: 'Bearer <token>' }];
160-
// const cookies = [];
161-
162-
// const expectedData = { schemaSDL: 'schema sdl', clientSchema: 'client schema' };
163-
// const mockIntrospectionQuery = { __schema: { types: [] } };
164-
// // jest.spyOn(graphQLController, 'buildClientSchema').mockReturnValueOnce('client schema');
165-
// jest.spyOn(graphQLController, 'printSchema').mockReturnValueOnce('schema sdl');
166-
// jest.spyOn(graphQLController, 'introspectionDataChanged').mockReturnValueOnce('introspectionDataChanged action');
167-
// jest.spyOn(api, 'receive').mockImplementationOnce((eventName, callback) => callback(mockIntrospectionQuery));
168-
169-
// graphQLController.introspect(url, headers, cookies);
170-
171-
// expect(api.send).toHaveBeenCalledWith('introspect', JSON.stringify({ url, headers, cookies }));
172-
// // expect(graphQLController.buildClientSchema).toHaveBeenCalledWith(mockIntrospectionQuery);
173-
// expect(graphQLController.buildClientSchema).toHaveBeenCalledWithTimes(1);
174-
// // expect(graphQLController.printSchema).toHaveBeenCalledWith('client schema');
175-
// expect(graphQLController.printSchema).toHaveBeenCalledTimes(1);
176-
// expect(appDispatch).toHaveBeenCalledWith(introspectionDataChanged(expectedData));
177-
// });
159+
// this test was previously commented out and fails
160+
it('should send introspection query and dispatch introspectionDataChanged action', () => {
161+
const url = 'http://localhost:4000/graphql';
162+
const headers = [{ key: 'Authorization', value: 'Bearer <token>' }];
163+
const cookies = [];
164+
165+
const expectedData = { schemaSDL: 'schema sdl', clientSchema: 'client schema' };
166+
const mockIntrospectionQuery = { __schema: { types: [] } };
167+
jest.spyOn(graphQLController, 'buildClientSchema').mockReturnValueOnce('client schema');
168+
jest.spyOn(graphQLController, 'printSchema').mockReturnValueOnce('schema sdl');
169+
jest.spyOn(graphQLController, 'introspectionDataChanged').mockReturnValueOnce('introspectionDataChanged action');
170+
jest.spyOn(api, 'receive').mockImplementationOnce((eventName, callback) => callback(mockIntrospectionQuery));
171+
172+
graphQLController.introspect(url, headers, cookies);
173+
174+
expect(api.send).toHaveBeenCalledWith('introspect', JSON.stringify({ url, headers, cookies }));
175+
expect(graphQLController.buildClientSchema).toHaveBeenCalledWith(mockIntrospectionQuery);
176+
expect(graphQLController.buildClientSchema).toHaveBeenCalledWithTimes(1);
177+
expect(graphQLController.printSchema).toHaveBeenCalledWith('client schema');
178+
expect(graphQLController.printSchema).toHaveBeenCalledTimes(1);
179+
expect(appDispatch).toHaveBeenCalledWith(introspectionDataChanged(expectedData));
180+
});
178181
});
179182

180183
// Test cases for openGraphQLConnection
@@ -189,7 +192,7 @@ describe('graphQLController', () => {
189192
timeSent: Date.now(),
190193
};
191194

192-
xit('should initialize response data correctly', async () => {
195+
it('should initialize response data correctly', async () => {
193196
await graphQLController.openGraphQLConnection(reqResObj);
194197

195198
expect(reqResObj.response.headers).toEqual({});
@@ -240,7 +243,7 @@ describe('graphQLController', () => {
240243
/* create an array of ReqRes objects */
241244
];
242245

243-
xit('should call runSingleGraphQLRequest with the first object in the array', async () => {
246+
it('should call runSingleGraphQLRequest with the first object in the array', async () => {
244247
const runSingleGraphQLRequestSpy = jest.spyOn(
245248
graphQLController,
246249
'runSingleGraphQLRequest'
@@ -251,17 +254,18 @@ describe('graphQLController', () => {
251254
expect(runSingleGraphQLRequestSpy).toHaveBeenCalledWith(reqResArray[0]);
252255
});
253256

254-
// it('should call runSingleGraphQLRequest with the next object in the array after the first object has received a response', async () => {
255-
// const response = { /* create a GraphQLResponse object */ };
256-
// const sendGqlToMainSpy = jest.spyOn(graphQLController, 'sendGqlToMain')
257-
// .mockResolvedValue({ data: response, reqResObj: reqResArray[0] });
257+
// this test was previously commented out and fails
258+
it('should call runSingleGraphQLRequest with the next object in the array after the first object has received a response', async () => {
259+
const response = { /* create a GraphQLResponse object */ };
260+
const sendGqlToMainSpy = jest.spyOn(graphQLController, 'sendGqlToMain')
261+
.mockResolvedValue({ data: response, reqResObj: reqResArray[0] });
258262

259-
// const runSingleGraphQLRequestSpy = jest.spyOn(graphQLController, 'runSingleGraphQLRequest');
263+
const runSingleGraphQLRequestSpy = jest.spyOn(graphQLController, 'runSingleGraphQLRequest');
260264

261-
// await graphQLController.openGraphQLConnectionAndRunCollection(reqResArray);
265+
await graphQLController.openGraphQLConnectionAndRunCollection(reqResArray);
262266

263-
// expect(runSingleGraphQLRequestSpy).toHaveBeenCalledWith(reqResArray[1]);
264-
// });
267+
expect(runSingleGraphQLRequestSpy).toHaveBeenCalledWith(reqResArray[1]);
268+
});
265269
});
266270
});
267271

0 commit comments

Comments
 (0)