Skip to content

Commit db6554e

Browse files
committed
added handleError and handleResponse tests
1 parent 16aec5f commit db6554e

File tree

2 files changed

+106
-89
lines changed

2 files changed

+106
-89
lines changed

src/client/controllers/graphQLController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ const graphQLController: GqlController = {
6464
// send reqRes object to main process through context bridge
6565
this.sendGqlToMain({ reqResObj })
6666
.then((response) => {
67-
if (response.error)
67+
if (response.error) {
6868
this.handleError(response.reqResObj.error, response.reqResObj);
69+
}
70+
6971
else this.handleResponse(response.data, response.reqResObj);
7072
})
7173
.catch((err) => console.log('error in sendGqlToMain', err));

test/__tests__/graphQLControllerTest-Brooke.js

Lines changed: 103 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,137 @@ import graphQLController from '../../src/client/controllers/graphQLController.ts
44
appear in the graphQLController file -Brooke*/
55
describe('graphQLController', () => {
66

7-
/* the openGraphQLConnection function includes calls to sendGqlMain and handleRespnose, I created mocks for those functions
8-
to fully isolate openGraphQL functionality */
7+
/* the openGraphQLConnection function includes calls to sendGqlMain, handleRespnose and handleError, I created mocks for those functions
8+
to fully isolate openGraphQL functionality, mocks are local to each test suite */
99
describe('openGraphQLConnection', () => {
1010

1111
beforeAll(() => {
1212
graphQLController.sendGqlToMain = jest.fn().mockResolvedValue({}); // utilized mockResolvedValue since sendGqlToMain returns a promise
13-
graphQLController.handleResponse = jest.fn();
13+
graphQLController.handleResponse = jest.fn();
14+
graphQLController.handleError = jest.fn();
1415
})
1516

1617
afterEach(() => {
1718
graphQLController.sendGqlToMain.mockClear();
1819
graphQLController.handleResponse.mockClear();
20+
graphQLController.handleError.mockClear();
1921
})
2022

2123
it('should initialize response data correctly', async () => {
2224
const reqResObj = {};
2325
await graphQLController.openGraphQLConnection(reqResObj);
2426

2527
/* 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 */
28+
before being passed into the mock sendGqltoMain function, previously this test checked the properties of a hardcoded object */
2729
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.headers).toEqual({});
2830
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.events).toEqual([]);
2931
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.response.cookies).toEqual([]);
3032
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.connection).toEqual('open');
3133
expect(graphQLController.sendGqlToMain.mock.calls[0][0].reqResObj.timeSent).toBeGreaterThan(0);
3234
});
3335

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-
})
36+
it ('should call sendGqlToMain' , async () => {
37+
await graphQLController.openGraphQLConnection({});
38+
expect(graphQLController.sendGqlToMain).toBeCalledTimes(1);
39+
});
40+
})
41+
42+
/* check handleError and handleResponse branches after sendGqlToMain call */
43+
it('should call handleError if sendGqlToMain returns an object with an error property', async () => {
44+
// updated resolved value of sendGqlToMain to include an error that triggers the error conditional
45+
graphQLController.sendGqlToMain = jest.fn().mockResolvedValue({error: true, reqResObj: {error: true}})
46+
await graphQLController.openGraphQLConnection({});
47+
expect(graphQLController.handleError).toBeCalledTimes(1);
48+
});
49+
50+
it('should call handleResponse if sendGqlToMain returns an object without an error property',
51+
async () => {
52+
// reassigned resolved value to empty object following previous test case
53+
graphQLController.sendGqlToMain = jest.fn().mockResolvedValue({})
54+
await graphQLController.openGraphQLConnection({});
55+
expect(graphQLController.handleResponse).toBeCalledTimes(1);
56+
});
57+
});
4358

59+
/* the openGraphQLConnetionAndRunCollection function is run when the reqResObj at the first reqResArray index is graphQL and
60+
the user selects 'test collection' (instead of 'send test'), this functionality currently does not work and needs refactoring,
61+
the tests should be written alongside this process -Brooke */
62+
describe('openGraphQLConnectionAndRunCollection', () => {});
4463

45-
// describe('sendGqlToMain', () => {
46-
47-
// })
64+
describe('sendGqlToMain', () => {});
4865

49-
// describe('openSubscription', () => {
50-
51-
// })
66+
describe('openSubscription', () => {});
5267

53-
// describe('closeSubscription', () => {
54-
55-
// })
56-
// describe('handleResponse', () => {
57-
58-
// })
68+
describe('closeSubscription', () => {});
69+
70+
describe('handleResponse', () => {});
5971

60-
// describe('handleError', () => {
61-
62-
// })
72+
describe('handleError', () => {});
6373

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-
// ];
74+
describe('cookieFormatter', () => {
75+
76+
/* test written by previous iteration group that hardcodes an input cookie array and expected output array
77+
and checks if the output of graphQLController.cookieformatter(input) returns the expected output */
78+
it('should format cookie array', () => {
79+
const cookieArray = [
80+
{
81+
domain: 'localhost',
82+
expires: '2024-06-12T18:19:03.262Z',
83+
hostOnly: true,
84+
httpOnly: false,
85+
name: 'cookie1',
86+
path: '/',
87+
secure: false,
88+
session: false,
89+
value: 'value1',
90+
},
91+
{
92+
domain: 'localhost',
93+
expires: '2024-06-12T18:19:03.262Z',
94+
hostOnly: true,
95+
httpOnly: false,
96+
name: 'cookie2',
97+
path: '/',
98+
secure: false,
99+
session: false,
100+
value: 'value2',
101+
},
102+
];
103+
104+
const expectedOutput = [
105+
{
106+
domain: 'localhost',
107+
expires: '2024-06-12T18:19:03.262Z',
108+
hostOnly: true,
109+
httpOnly: false,
110+
name: 'cookie1',
111+
path: '/',
112+
secure: false,
113+
session: false,
114+
value: 'value1',
115+
},
116+
{
117+
domain: 'localhost',
118+
expires: '2024-06-12T18:19:03.262Z',
119+
hostOnly: true,
120+
httpOnly: false,
121+
name: 'cookie2',
122+
path: '/',
123+
secure: false,
124+
session: false,
125+
value: 'value2',
126+
},
127+
];
128+
129+
expect(graphQLController.cookieFormatter(cookieArray)).toEqual(expectedOutput);
119130

120-
// expect(graphQLController.cookieFormatter(cookieArray)).toEqual(
121-
// expectedOutput
122-
// );
123-
// });
131+
});
132+
133+
// all of the test cases that were in previous tests were commented out and failed when recommented in, needs refactoring
134+
describe('introspect', () => {})
135+
136+
});
124137

125-
// })
138+
139+
140+

0 commit comments

Comments
 (0)