Return only single selected story, when it not matches to search term and not the full kind

This commit is contained in:
igor 2017-08-17 11:52:46 +03:00
parent 4a094e1408
commit 2e5ccc628a
3 changed files with 42 additions and 15 deletions

View File

@ -21,6 +21,7 @@ export const mapper = (state, props, { actions }) => {
preparedStories,
storyFilter,
selectedKind,
selectedStory,
sortStoriesByKind
);

View File

@ -34,16 +34,16 @@ function flattenStories(items) {
}, []);
}
function applySearchHookForSelectedKind(stories, filter, selectedKind) {
function applySearchHookForSelectedKind(stories, filter, selectedKind, selectedStory) {
return stories.map(story => {
if (story.kind !== selectedKind) {
return story;
if (story.kind === selectedKind && story.storyName === selectedStory) {
return {
...story,
searchHook: filter,
};
}
return {
...story,
searchHook: filter,
};
return story;
});
}
@ -81,7 +81,7 @@ function groupStories(matchedItems) {
return Array.from(storiesMap.values());
}
export function storyFilter(stories, filter, selectedKind, sortStoriesByKind) {
export function storyFilter(stories, filter, selectedKind, selectedStory, sortStoriesByKind) {
if (!stories) {
return null;
}
@ -93,7 +93,14 @@ export function storyFilter(stories, filter, selectedKind, sortStoriesByKind) {
}
const flattened = flattenStories(sorted);
const storiesWithHook = applySearchHookForSelectedKind(flattened, filter, selectedKind);
const storiesWithHook = applySearchHookForSelectedKind(
flattened,
filter,
selectedKind,
selectedStory
);
const fuse = new Fuse(storiesWithHook, searchOptions);
const foundStories = fuse.search(filter);

View File

@ -18,12 +18,27 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'aa', namespaces: ['aa'], stories: ['bb'] },
{ kind: 'bb', namespaces: ['bb'], stories: ['bb'] },
];
const selectedKind = 'bb';
const res = storyFilter(stories, 'no-match', selectedKind);
const selectedStory = 'bb';
const res = storyFilter(stories, 'no-match', selectedKind, selectedStory);
expect(res).toMatchObject([stories[1]]);
});
test('should always return the selectedKind with the single selectedStory', () => {
const stories = [
{ kind: 'aa', namespaces: ['aa'], stories: ['bb'] },
{ kind: 'bb', namespaces: ['bb'], stories: ['bb', 'cc', 'dd'] },
];
const selectedKind = 'bb';
const selectedStory = 'cc';
const res = storyFilter(stories, 'no-match', selectedKind, selectedStory);
expect(res[0].stories).toEqual(['cc']);
});
test('should filter kinds correctly', () => {
const stories = [
{ kind: 'aa', namespaces: ['aa'], stories: ['bb'] },
@ -31,7 +46,8 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'ss', namespaces: ['ss'], stories: ['bb'] },
];
const selectedKind = 'bb';
const res = storyFilter(stories, 'aa', selectedKind);
const selectedStory = 'bb';
const res = storyFilter(stories, 'aa', selectedKind, selectedStory);
expect(res).toMatchObject([stories[0], stories[1]]);
});
@ -53,7 +69,7 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'aa', namespaces: ['aa'], stories: ['bb'] },
{ kind: 'bb', namespaces: ['bb'], stories: ['bb'] },
];
const res = storyFilter(stories, null, null, true);
const res = storyFilter(stories, null, null, null, true);
expect(res).toEqual([stories[1], stories[2], stories[0]]);
});
@ -65,7 +81,8 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'ee', namespaces: ['ee'], stories: ['ff'] },
];
const selectedKind = 'aa';
const res = storyFilter(stories, 'ff', selectedKind);
const selectedStory = 'bb';
const res = storyFilter(stories, 'ff', selectedKind, selectedStory);
expect(res).toMatchObject([stories[0], stories[2]]);
});
@ -77,7 +94,8 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'ee', namespaces: ['ee'], stories: ['ff', 'gg'] },
];
const selectedKind = 'aa';
const res = storyFilter(stories, 'ff', selectedKind);
const selectedStory = 'bb';
const res = storyFilter(stories, 'ff', selectedKind, selectedStory);
expect(res).toMatchObject([stories[0], { kind: 'ee', stories: ['ff'] }]);
});
@ -99,7 +117,8 @@ describe('manager.ui.libs.filters', () => {
{ kind: 'cc', namespaces: ['cc'], stories: ['dd', 'eE'] },
];
const selectedKind = 'aa';
const res = storyFilter(stories, 'ee', selectedKind);
const selectedStory = 'bb';
const res = storyFilter(stories, 'ee', selectedKind, selectedStory);
expect(res).toMatchObject([stories[0], { kind: 'cc', stories: ['eE'] }]);
});