Skip to content

Commit

Permalink
clean redundant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EraliaKats committed Dec 24, 2024
1 parent 8451195 commit b4a13b3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 442 deletions.
75 changes: 45 additions & 30 deletions spec/javascript/utility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,53 +76,68 @@ describe('Utility.countrySelectorOnConfirm', () => {

describe('Utility.fetchCommoditySearchSuggestions', () => {
beforeEach(() => {
global.fetch = jest.fn();
});

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

it('fetches suggestions and populates results', async () => {
const query = 'wine';
const searchSuggestionsPath = '/search-suggestions';
const options = [];
const populateResults = jest.fn();

const mockResponse = {
results: [
{id: 'wine', text: 'wine', query: 'wine', resource_id: '6828', formatted_suggestion_type: ''},
{id: 'red wine', text: 'red wine', query: 'wine', resource_id: '7273', formatted_suggestion_type: ''},
],
};

global.fetch.mockResolvedValue({
json: jest.fn().mockResolvedValue(mockResponse),
});

const query = 'wine';
const searchSuggestionsPath = '/test-path';
const options = [];
const populateResults = jest.fn();

await Utility.fetchCommoditySearchSuggestions(query, searchSuggestionsPath, options, populateResults);

const expectedResults = [
{id: 'wine', text: 'wine', suggestion_type: 'exact', newOption: true},
...mockResponse.results,
];

expect(options).toEqual(expectedResults);
'wine', 'red wine'
]

global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve(mockResponse),
})
);

await Utility.fetchCommoditySearchSuggestions(
query,
searchSuggestionsPath,
options,
populateResults
);

expect(populateResults).toHaveBeenCalledWith([
'wine',
'red wine'
]);

expect(populateResults).toHaveBeenCalledWith(
expect.arrayContaining(expectedResults)
);
});

it('handles fetch error gracefully', async () => {
global.fetch.mockRejectedValue(new Error('Fetch error'));
const mockQuery = 'wine';
const mockSearchSuggestionsPath = '/search-suggestions';
const mockOptions = {}; // Any additional options if needed
const mockPopulateResults = jest.fn();

const query = 'wine';
const searchSuggestionsPath = '/test-path';
const options = [];
const populateResults = jest.fn();
global.fetch = jest.fn(() => Promise.reject(new Error('Network error')));

await Utility.fetchCommoditySearchSuggestions(query, searchSuggestionsPath, options, populateResults);
const mockDispatchEvent = jest.spyOn(document, 'dispatchEvent');

expect(populateResults).toHaveBeenCalledWith([]);
});
});
await Utility.fetchCommoditySearchSuggestions(
mockQuery,
mockSearchSuggestionsPath,
mockOptions,
mockPopulateResults
);

expect(mockPopulateResults).toHaveBeenCalledWith([]);
expect(mockDispatchEvent).not.toHaveBeenCalled();

describe('Utility.commoditySelectorOnConfirm', () => {
let options; let resourceIdHidden; let inputElement; let form;
Expand Down
67 changes: 0 additions & 67 deletions spec/requests/search_spec.rb

This file was deleted.

106 changes: 0 additions & 106 deletions spec/vcr/searching_exact_match.yml

This file was deleted.

Loading

0 comments on commit b4a13b3

Please sign in to comment.