Skip to content

Commit

Permalink
fix git history
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivian Xiao committed Jul 19, 2024
1 parent 3968a36 commit e2cb578
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function scaleBetween(
originalScaleMin: number,
originalScaleMax: number
): number {
// returns midpoint of new range if original range is 0
// returns midpoint of new range if original range is of size 0
if (originalScaleMax === originalScaleMin)
return newScaleMin + (newScaleMax - newScaleMin) / 2;
return (
Expand Down
56 changes: 56 additions & 0 deletions packages/compass-query-bar/src/stores/query-bar-reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
changeField,
explainQuery,
resetQuery,
saveRecentAsFavorite,
setQuery,
} from './query-bar-reducer';
import { configureStore } from './query-bar-store';
Expand All @@ -22,6 +23,7 @@ import type { PreferencesAccess } from 'compass-preferences-model';
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
import { createNoopLogger } from '@mongodb-js/compass-logging/provider';
import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider';
import { waitFor } from '@testing-library/react';

function createStore(
opts: Partial<RootState['queryBar']> = {},
Expand All @@ -43,6 +45,10 @@ describe('queryBarReducer', function () {
} as QueryBarExtraArgs);
});

afterEach(function () {
Sinon.restore();
});

describe('changeField', function () {
const specs: [QueryProperty, string, unknown, boolean][] = [
['filter', '{ foo: true }', { foo: true }, true],
Expand Down Expand Up @@ -163,6 +169,56 @@ describe('queryBarReducer', function () {
'lastAppliedQuery.query.test'
);
});

it('should not re-save favorite query in recents', async function () {
const updateAttributesStub = Sinon.stub();
const saveQueriesStub = Sinon.stub().resolves();
const loadAllStub = Sinon.stub().resolves([
{ filter: { _id: '123' }, limit: 10 },
]);
const favoriteQueriesStorage = {
updateAttributes: updateAttributesStub,
loadAll: loadAllStub,
};
const recentQueriesStorage = {
saveQuery: saveQueriesStub,
};
preferences = await createSandboxFromDefaultPreferences();
const store = createStore({}, {
preferences,
logger: createNoopLogger(),
track: createNoopTrack(),
favoriteQueryStorage: favoriteQueriesStorage,
recentQueryStorage: recentQueriesStorage,
} as any);

const queryAction = setQuery({ filter: { _id: '123' }, limit: 10 });
store.dispatch(queryAction);
store.dispatch(applyQuery('test'));
await waitFor(() => {
expect(saveQueriesStub.calledOnce).to.be.true;
});
await store.dispatch(
saveRecentAsFavorite(saveQueriesStub.firstCall.firstArg, 'favorite')
);
await waitFor(() => {
expect(loadAllStub.called).to.be.true;
});
const appliedQuery = store.dispatch(applyQuery('test'));

expect(appliedQuery).to.deep.eq({
...DEFAULT_QUERY_VALUES,
filter: { _id: '123' },
limit: 10,
});
expect(store.getState().queryBar)
.to.have.nested.property('lastAppliedQuery.query.test')
.deep.eq(appliedQuery);

// updateAttributes is called in saveRecentAsFavorite and updateFavoriteQuery
expect(updateAttributesStub).to.have.been.calledTwice;
expect(saveQueriesStub).not.to.have.been.calledTwice;
});
});

describe('resetQuery', function () {
Expand Down

0 comments on commit e2cb578

Please sign in to comment.