Search test pt.2
Published: 2022-02-21 22:52:46

If you haven't read my part 1, [Link here](https://dev.to/jiahuazou/search-test-pt1-3m27). TLDR, it's all about the connection to a mock Elasticsearch and the problems I faced.

## Tests
This time it's all about the testing that I have created ([Github PR](https://github.com/Seneca-CDOT/telescope/pull/2927)). So creating the test was very easy once I got the mock connection working. My focus for these are makin sure query are sending the proper text and receiving proper data. The first setup I did was to create the `describe` for the normal search and then I put in all of the individual tests with `it` function like this: 
```js
describe('/query routers', () => {
  it('return error 400 if no params given', async () => {
    const res = await request(app).get('/');
    expect(res.status).toBe(400);
    expect(res.statusCode).toBe(400);
  });
});
```
This first test was create before([Link to blog](https://dev.to/jiahuazou/prerelease-26-2imh)) when I setup the initial testing. Before adding different test, I have to make sure the mock has to be clear before each tests. To do that, jest has a function that can easy perform this like so: 
```js
afterEach(() => {
    mock.clearAll();
  });
```

### Testing for Statuscode
After that, my first few test will be related to anything with the status code `400` or `503`. To cause the status code `400`, the query send will have mistakes. So the query `filter` has to be either `post` or `author` anything else will cause a status `400`. Also if the `text` query is empty it will also return the same error. [code block](https://github.com/Seneca-CDOT/telescope/pull/2927/commits/a197decade6692242fbe1d9bda8abae17e3e4e56#diff-eca368c10a5833d8ee7d7898c68facc74568ca76a10b8ddee439a74f287c2ac1R35-R74).

```js
returns for status code 400
 () => {
        return {};
      }

//proper return
() => {
        return {
          results: 0,
          hits: {
            total: { value: 0 },
            hits: [],
          },
        };
      }
```
If your wondering why I have it return nothing and not return with a proper format(returning `results` and `hits`), it because the query isn't proper sent correctly, the result values will nothing but an error. 

If the query was sent correctly, and it returns not a proper format it will cause a error `503` because the search results is expecting a `results` and `hits`.[Error 503 test](https://github.com/Seneca-CDOT/telescope/pull/2927/commits/a197decade6692242fbe1d9bda8abae17e3e4e56#diff-eca368c10a5833d8ee7d7898c68facc74568ca76a10b8ddee439a74f287c2ac1R165-R178). [request format](https://github.com/Seneca-CDOT/telescope/blob/master/src/api/search/src/search.js#L75-L78)

### Testing for results
After testing the error `400` and `503` now I can start testing for results. There are only 2 tests, one for `post` and the other for `author`. [Tests for them](https://github.com/Seneca-CDOT/telescope/pull/2927/commits/a197decade6692242fbe1d9bda8abae17e3e4e56#diff-eca368c10a5833d8ee7d7898c68facc74568ca76a10b8ddee439a74f287c2ac1R97-R163). So for these tests, I made sure that I can expect a status code `200` with results and total number of results.
This part was a little tricky because if any of the format is wrong, it will cause a status code `503`. The return must have a `results` with a `hits`. Within that `hits` it will display the `total` result values with a list of `url` link with the `id`. This is the format that it needs to return.
```js
() => {
        return {
          results: 2,
          hits: {
            total: { value: 2 },
            hits: [
              {
                _id: '1234',
                url: `${POSTS_URL}/`,
              },
              {
                _id: '5678',
                url: `${POSTS_URL}/`,
              },
            ],
          },
        };
```

## Conclusion
Tackling this issue felt it was hard and easy. Mostly the mock Elasticsearch connection was the most challenging for me. It did felt very rewarding when I was able to merge my work to the repo. After this I felt like I can do more issues related to `Elasticsearch`.