Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set viewMode to story when calling selectStory from non story pages #11317

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,18 @@ export const init: ModuleFn = ({
const s = hash[kindOrId] || hash[sanitize(kindOrId)];
// eslint-disable-next-line no-nested-ternary
const id = s ? (s.children ? s.children[0] : s.id) : kindOrId;
const viewMode =
let viewMode =
viewModeFromArgs || (s && s.parameters.viewMode)
? s.parameters.viewMode
: viewModeFromState;

// In some cases, the viewMode could be something other than docs/story
// ('settings', for example) and therefore we should make sure we go back
// to the 'story' viewMode when navigating away from those pages.
if (!viewMode.match(/docs|story/)) {
viewMode = 'story';
}

const p = s && s.refId ? `/${viewMode}/${s.refId}_${id}` : `/${viewMode}/${id}`;

navigate(p);
Expand Down
14 changes: 14 additions & 0 deletions lib/api/src/tests/stories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,20 @@ describe('stories API', () => {
expect(navigate).toHaveBeenCalledWith('/story/a--2');
});

it('allows navigating away from the settings pages', () => {
const navigate = jest.fn();
const store = {
getState: () => ({ viewMode: 'settings', storyId: 'about', storiesHash }),
};

const {
api: { selectStory },
} = initStories({ store, navigate, provider });

selectStory('a--2');
expect(navigate).toHaveBeenCalledWith('/story/a--2');
});

it('allows navigating to first story in kind on call by kind', () => {
const navigate = jest.fn();
const store = createMockStore();
Expand Down