Skip to content

Commit 9aad997

Browse files
committed
completed testing on Test New Query page functionality
1 parent 35bba2a commit 9aad997

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/pages/TestNewQuery.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ const TestNewQuery: React.FC = () => {
228228
};
229229

230230
const saveQuery = async () => {
231+
console.log('Testing In Save Query ⭐️');
231232
try {
232233
// TODO remove commented out if not used selectedDb later
233234
// conditional to check that a query was run and that it had results
@@ -236,6 +237,7 @@ const TestNewQuery: React.FC = () => {
236237
alert('Please ensure you have ran a query and you received results');
237238
return;
238239
}
240+
console.log('Testing After If ⭐️');
239241

240242
// the BE returns back formatted query results
241243
// we want to extract just the data portion and send to the BE to save the query - since it's in str format, convert to obj

tests/components/TestNewQuery.spec.tsx

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Test New Query Component', () => {
2323
it('renders the Test New Query heading', () => {
2424
renderWithRouter(<TestNewQuery />);
2525
// will check if the heading 'Test Your Query!' is rendering on the screen
26-
expect(screen.getByText(/Test Your Query!/i)).toBeInTheDocument();
26+
expect(screen.getByRole('heading', { name: /Test New Query/i })).toBeInTheDocument();
2727
});
2828

2929
//? Testing (2): input fields render
@@ -106,13 +106,28 @@ describe('Test New Query Component', () => {
106106
//? Testing (6): clicking Save Query button triggers Post Req
107107
it('checks if clicking Save Query button triggers req to the BE', async () => {
108108
// mocking running a query so we have queryResult
109-
const getMockResponse: AxiosResponse<string[]> = {
110-
data: [
111-
'Name: My Test Query',
112-
'Query: SELECT * FROM users',
113-
'Date Run: 2025-04-11',
114-
'Execution Time: 0.42',
115-
],
109+
const getMockResponse: AxiosResponse<{ metrics: string[]; otherMetrics: any[] }> = {
110+
data: {
111+
metrics: [
112+
'Query Name: Testing Sunday',
113+
'Query: SELECT * FROM people',
114+
'Date Run: 2025-04-11',
115+
'Execution Time: 0.042',
116+
],
117+
otherMetrics: [
118+
{
119+
planningTime: 1.932,
120+
actualTotalTime: 0.025,
121+
totalCost: 2.87,
122+
nodeType: 'Seq Scan',
123+
relationName: 'people',
124+
planRows: 87,
125+
actualRows: 87,
126+
sharedHit: 2,
127+
sharedRead: 0,
128+
},
129+
],
130+
},
116131
status: 200,
117132
statusText: 'OK',
118133
headers: {},
@@ -128,7 +143,7 @@ describe('Test New Query Component', () => {
128143
};
129144
// mocking the axios Get Req w/ a mocked response
130145
const mockGet = jest.spyOn(axios, 'get').mockResolvedValue(getMockResponse);
131-
const mockPost = jest.spyOn(axios, 'post').mockResolvedValue({ getMockResponse });
146+
const mockPost = jest.spyOn(axios, 'post').mockResolvedValue(postMockResponse);
132147

133148
renderWithRouter(<TestNewQuery />);
134149

@@ -171,14 +186,29 @@ describe('Test New Query Component', () => {
171186
it('checks if clicking Run Query button renders a table with results', async (): Promise<void> => {
172187
// mocking the axios Get Req w/ a mocked response
173188
// using TS to define the response
174-
const mockData: string[] = [
175-
'Query Name: My Test Query',
176-
'Query: SELECT * FROM users',
177-
'Query Date: 2025-04-11',
178-
'Exec Time: 0.42',
179-
];
189+
const mockData = {
190+
metrics: [
191+
'Query Name: My Test Query',
192+
'Query: SELECT * FROM users',
193+
'Date Run: 2025-04-11',
194+
'Execution Time: 0.042',
195+
],
196+
otherMetrics: [
197+
{
198+
planningTime: 1.932,
199+
actualTotalTime: 0.025,
200+
totalCost: 2.87,
201+
nodeType: 'Seq Scan',
202+
relationName: 'people',
203+
planRows: 87,
204+
actualRows: 87,
205+
sharedHit: 2,
206+
sharedRead: 0,
207+
},
208+
],
209+
};
180210
// using TS to define the response
181-
const mockResponse: AxiosResponse<string[]> = {
211+
const mockResponse: AxiosResponse<{ metrics: string[]; otherMetrics: any[] }> = {
182212
data: mockData,
183213
status: 200,
184214
statusText: 'OK',
@@ -210,6 +240,7 @@ describe('Test New Query Component', () => {
210240
});
211241

212242
await userEvent.click(runQueryButton);
243+
screen.debug();
213244

214245
// will wait for QUery Results heading to render
215246
await waitFor(() => {
@@ -220,7 +251,7 @@ describe('Test New Query Component', () => {
220251
expect(screen.getByText(/My Test Query/i)).toBeInTheDocument();
221252
expect(screen.getByText(/SELECT \* FROM users/i)).toBeInTheDocument();
222253
expect(screen.getByText(/2025-04-11/i)).toBeInTheDocument();
223-
expect(screen.getByText(/0.42/i)).toBeInTheDocument();
254+
expect(screen.getByText(/0.042/i)).toBeInTheDocument();
224255

225256
// remove mock before next test
226257
mockGet.mockRestore();

0 commit comments

Comments
 (0)