2017-08-17 22:00:37 +05:30
|
|
|
import RecentSearchesStore from '~/filtered_search/stores/recent_searches_store';
|
|
|
|
|
|
|
|
describe('RecentSearchesStore', () => {
|
|
|
|
let store;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
store = new RecentSearchesStore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addRecentSearch', () => {
|
|
|
|
it('should add to the front of the list', () => {
|
|
|
|
store.addRecentSearch('foo');
|
|
|
|
store.addRecentSearch('bar');
|
|
|
|
|
|
|
|
expect(store.state.recentSearches).toEqual(['bar', 'foo']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should deduplicate', () => {
|
|
|
|
store.addRecentSearch('foo');
|
|
|
|
store.addRecentSearch('bar');
|
|
|
|
store.addRecentSearch('foo');
|
|
|
|
|
|
|
|
expect(store.state.recentSearches).toEqual(['foo', 'bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only keeps track of 5 items', () => {
|
|
|
|
store.addRecentSearch('1');
|
|
|
|
store.addRecentSearch('2');
|
|
|
|
store.addRecentSearch('3');
|
|
|
|
store.addRecentSearch('4');
|
|
|
|
store.addRecentSearch('5');
|
|
|
|
store.addRecentSearch('6');
|
|
|
|
store.addRecentSearch('7');
|
|
|
|
|
|
|
|
expect(store.state.recentSearches).toEqual(['7', '6', '5', '4', '3']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setRecentSearches', () => {
|
|
|
|
it('should override list', () => {
|
2018-12-13 13:39:08 +05:30
|
|
|
store.setRecentSearches(['foo', 'bar']);
|
|
|
|
store.setRecentSearches(['baz', 'qux']);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(store.state.recentSearches).toEqual(['baz', 'qux']);
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('handles non-string values', () => {
|
|
|
|
store.setRecentSearches(['foo ', { foo: 'bar' }, { foo: 'bar' }, ['foobar']]);
|
|
|
|
|
|
|
|
// 1. String values will be trimmed of leading/trailing spaces
|
|
|
|
// 2. Comparison will account for objects to remove duplicates
|
|
|
|
// 3. Old behaviour of handling string values stays as it is.
|
|
|
|
expect(store.state.recentSearches).toEqual(['foo', { foo: 'bar' }, ['foobar']]);
|
|
|
|
});
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it('only keeps track of 5 items', () => {
|
|
|
|
store.setRecentSearches(['1', '2', '3', '4', '5', '6', '7']);
|
|
|
|
|
|
|
|
expect(store.state.recentSearches).toEqual(['1', '2', '3', '4', '5']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|