Skip to content

Commit

Permalink
feat(connector): add tests for connectClearAll and connectHierarchica…
Browse files Browse the repository at this point in the history
…lMenu

Also straighten up the API on connectClearAll, to make it more
consistent between the initial rendering and the other renders
  • Loading branch information
Alexandre Stanislawski committed Feb 20, 2017
1 parent 5409157 commit 0eb29ec
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 50 deletions.
48 changes: 6 additions & 42 deletions src/connectors/clear-all/__tests__/connectClearAll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SearchResults = jsHelper.SearchResults;

import connectClearAll from '../connectClearAll.js';

describe.only('connectClearAll', () => {
describe('connectClearAll', () => {
it('Renders during init and render', () => {
const helper = jsHelper({});
helper.search = sinon.stub();
Expand Down Expand Up @@ -86,9 +86,6 @@ describe.only('connectClearAll', () => {
const initClearMethod = rendering.lastCall.args[0].clearAll;
initClearMethod();

// The clearing function only works when results are received
// At this point of the lifecycle we don't have results yet
// that's why the search state still contains refinements
expect(helper.hasRefinements('myFacet')).toBe(true);

helper.toggleRefinement('myFacet', 'someOtherValue');
Expand All @@ -100,10 +97,10 @@ describe.only('connectClearAll', () => {
createURL: () => '#',
});

// expect(helper.hasRefinements('myFacet')).toBe(true);
// const renderClearMethod = rendering.lastCall.args[0].clearAll;
// renderClearMethod();
// expect(helper.hasRefinements('myFacet')).toBe(false);
expect(helper.hasRefinements('myFacet')).toBe(true);
const renderClearMethod = rendering.lastCall.args[0].clearAll;
renderClearMethod();
expect(helper.hasRefinements('myFacet')).toBe(false);
});

it('some refinements from results <=> hasRefinements = true', () => {
Expand All @@ -127,7 +124,7 @@ describe.only('connectClearAll', () => {
onHistoryChange: () => {},
});

expect(rendering.lastCall.args[0].hasRefinements).toBe(false);
expect(rendering.lastCall.args[0].hasRefinements).toBe(true);

widget.render({
results: new SearchResults(helper.state, [{}]),
Expand Down Expand Up @@ -171,37 +168,4 @@ describe.only('connectClearAll', () => {

expect(rendering.lastCall.args[0].hasRefinements).toBe(false);
});

it('some refinements from results <=> hasRefinements = true', () => {
// test if the values sent to the rendering function
// are consistent with the search state
const helper = jsHelper({}, undefined, {facets: ['aFacet']});
helper.toggleRefinement('aFacet', 'some value');
helper.search = sinon.stub();

const container = document.createElement('div');
const rendering = sinon.stub();
const makeWidget = connectClearAll(rendering);
const widget = makeWidget({
container,
});

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

expect(rendering.lastCall.args[0].hasRefinements).toBe(false);

widget.render({
results: new SearchResults(helper.state, [{}]),
state: helper.state,
helper,
createURL: () => '#',
});

expect(rendering.lastCall.args[0].hasRefinements).toBe(true);
});
});
10 changes: 7 additions & 3 deletions src/connectors/clear-all/connectClearAll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
bemHelper,
getContainerNode,
getRefinements,
prepareTemplateProps,
clearRefinementsFromState,
clearRefinementsAndSearch,
Expand Down Expand Up @@ -51,13 +52,16 @@ const connectClearAll = renderClearAll => ({
init({helper, templatesConfig, createURL}) {
this.clearAll = this.clearAll.bind(this, helper);
this._templateProps = prepareTemplateProps({defaultTemplates, templatesConfig, templates});
this.clearAttributes = [];
this.clearAttributes = getRefinements({}, helper.state)
.map(one => one.attributeName)
.filter(one => excludeAttributes.indexOf(one) === -1);
const hasRefinements = this.clearAttributes.length !== 0;

renderClearAll({
clearAll: () => {},
collapsible,
cssClasses,
hasRefinements: false,
hasRefinements,
shouldAutoHideContainer: autoHideContainer,
templateProps: this._templateProps,
url: createURL(clearRefinementsFromState(helper.state)),
Expand All @@ -66,7 +70,7 @@ const connectClearAll = renderClearAll => ({
},

render({results, state, createURL}) {
this.clearAttributes = results.getRefinements()
this.clearAttributes = getRefinements(results, state)
.map(one => one.attributeName)
.filter(one => excludeAttributes.indexOf(one) === -1);
const hasRefinements = this.clearAttributes.length !== 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-env mocha */

import expect from 'expect';
import sinon from 'sinon';

import jsHelper from 'algoliasearch-helper';
const SearchResults = jsHelper.SearchResults;

import connectCurrentRefinedValues from '../connectCurrentRefinedValues.js';

describe('connectCurrentRefinedValues', () => {
it('Renders during init and render', () => {
const helper = jsHelper({});
helper.search = sinon.stub();
const container = document.createElement('div');
// test that the dummyRendering is called with the isFirstRendering
// flag set accordingly
const rendering = sinon.stub();
const makeWidget = connectCurrentRefinedValues(rendering);
const widget = makeWidget({
container,
});

expect(widget.getConfiguration).toBe(undefined);
// test if widget is not rendered yet at this point
expect(rendering.callCount).toBe(0);

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

// test that rendering has been called during init with isFirstRendering = true
expect(rendering.callCount).toBe(1);
expect(rendering.lastCall.args[1]).toBe(true);

const firstRenderingOptions = rendering.lastCall.args[0];
expect(firstRenderingOptions.containerNode).toBe(container);
expect(firstRenderingOptions.refinements).toEqual([]);
expect(firstRenderingOptions.collapsible).toBe(false);
expect(firstRenderingOptions.shouldAutoHideContainer).toBe(true);

widget.render({
results: new SearchResults(helper.state, [{}]),
state: helper.state,
helper,
createURL: () => '#',
});

// test that rendering has been called during init with isFirstRendering = false
expect(rendering.callCount).toBe(2);
expect(rendering.lastCall.args[1]).toBe(false);

const secondRenderingOptions = rendering.lastCall.args[0];
expect(secondRenderingOptions.containerNode).toBe(container);
expect(secondRenderingOptions.refinements).toEqual([]);
expect(secondRenderingOptions.collapsible).toBe(false);
expect(secondRenderingOptions.shouldAutoHideContainer).toBe(true);
});

it('Provide a function to clear the refinements at each step', () => {
// For each refinements we get a function that we can call
// for removing a single refinement
const helper = jsHelper({}, '', {
facets: ['myFacet'],
});
helper.search = sinon.stub();
const container = document.createElement('div');
const rendering = sinon.stub();
const makeWidget = connectCurrentRefinedValues(rendering);
const widget = makeWidget({
container,
});

helper.addFacetRefinement('myFacet', 'value');

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

const firstRenderingOptions = rendering.lastCall.args[0];
const clearFunctions = firstRenderingOptions.clearRefinementClicks;
const refinements = firstRenderingOptions.refinements;
expect(clearFunctions.length).toBe(1);
expect(refinements.length).toBe(1);
clearFunctions[0]();
expect(helper.hasRefinements('myFacet')).toBe(false);

helper.addFacetRefinement('myFacet', 'value');

widget.render({
results: new SearchResults(helper.state, [{}]),
state: helper.state,
helper,
createURL: () => '#',
});

const secondRenderingOptions = rendering.lastCall.args[0];
const otherClearFunctions = secondRenderingOptions.clearRefinementClicks;
const otherRefinements = secondRenderingOptions.refinements;
expect(otherClearFunctions.length).toBe(1);
expect(otherRefinements.length).toBe(1);
otherClearFunctions[0]();
expect(helper.hasRefinements('myFacet')).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,22 @@ const connectCurrentRefinedValues = renderCurrentRefinedValues => ({

const clearAllURL = createURL(clearRefinementsFromState(helper.state, restrictedTo));

const refinements = getFilteredRefinements({}, helper.state, attributeNames, onlyListedAttributes);
const clearRefinementURLs =
refinements.map(refinement => createURL(clearRefinementFromState(helper.state, refinement)));
const clearRefinementClicks = refinements.map(refinement => clearRefinement.bind(null, helper, refinement));

renderCurrentRefinedValues({
attributes: attributesObj,
clearAllClick: this._clearRefinementsAndSearch,
clearAllPosition: clearAll,
clearAllURL,
clearRefinementClicks: [],
clearRefinementURLs: [],
clearRefinementClicks,
clearRefinementURLs,
collapsible,
cssClasses,
refinements: [],
autoHideContainer,
refinements,
shouldAutoHideContainer: autoHideContainer,
templateProps: this._templateProps,
containerNode,
}, true);
Expand Down
Loading

0 comments on commit 0eb29ec

Please sign in to comment.