Skip to content

Commit

Permalink
set current version id on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
mirefly committed Jul 24, 2019
1 parent 46529ec commit 9e7b293
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
20 changes: 20 additions & 0 deletions src/pages/Browse/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe(__filename, () => {
const _loadVersionAndFile = ({
store = configureStore(),
version = fakeVersion,
setCurrentVersionId = true,
}) => {
store.dispatch(versionsActions.loadVersionInfo({ version }));
store.dispatch(
Expand All @@ -99,6 +100,12 @@ describe(__filename, () => {
version,
}),
);

if (setCurrentVersionId) {
store.dispatch(
versionsActions.setCurrentVersionId({ versionId: version.id }),
);
}
};

const setUpVersionFileUpdate = ({
Expand Down Expand Up @@ -173,6 +180,19 @@ describe(__filename, () => {
expect(_fetchVersion).toHaveBeenCalledWith({ addonId, versionId });
});

it('dispatches setCurrentVersionId on mount if the currentVersionId is unset', () => {
const version = fakeVersion;
const store = configureStore();
_loadVersionAndFile({ store, version, setCurrentVersionId: false });
const dispatch = spyOn(store, 'dispatch');

render({ store });

expect(dispatch).toHaveBeenCalledWith(
versionsActions.setCurrentVersionId({ versionId: version.id }),
);
});

it('renders a VersionFileViewer', () => {
const version = {
...fakeVersion,
Expand Down
12 changes: 12 additions & 0 deletions src/pages/Browse/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getVersionInfo,
isFileLoading,
viewVersionFile,
actions as versionsActions,
} from '../../reducers/versions';
import {
getLocalizedString,
Expand Down Expand Up @@ -47,6 +48,7 @@ type PropsFromState = {
file: VersionFile | null | void;
fileIsLoading: boolean;
version: Version | void | null;
currentVersionId: number | null | undefined | false;
};

export type Props = RouteComponentProps<PropsFromRouter> &
Expand Down Expand Up @@ -75,6 +77,7 @@ export class BrowseBase extends React.Component<Props> {
const {
_fetchVersion,
_fetchVersionFile,
currentVersionId,
dispatch,
file,
fileIsLoading,
Expand Down Expand Up @@ -103,6 +106,11 @@ export class BrowseBase extends React.Component<Props> {
return;
}

if (version && !currentVersionId) {
dispatch(versionsActions.setCurrentVersionId({ versionId: version.id }));
return;
}

if (version.selectedPath && !fileIsLoading && file === undefined) {
dispatch(
_fetchVersionFile({
Expand Down Expand Up @@ -189,6 +197,9 @@ const mapStateToProps = (
const { match } = ownProps;
const versionId = parseInt(match.params.versionId, 10);
const version = getVersionInfo(state.versions, versionId);
const currentVersionId = version
? state.versions.currentVersionId
: undefined;

return {
apiState: state.api,
Expand All @@ -199,6 +210,7 @@ const mapStateToProps = (
? isFileLoading(state.versions, versionId, version.selectedPath)
: false,
version,
currentVersionId,
};
};

Expand Down
61 changes: 40 additions & 21 deletions src/pages/Compare/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe(__filename, () => {
headVersionId = 3,
store = configureStore(),
version = fakeVersionWithDiff,
setCurrentVersionId = true,
}) => {
store.dispatch(
versionsActions.loadVersionInfo({
Expand Down Expand Up @@ -144,6 +145,11 @@ describe(__filename, () => {
version,
}),
);
if (setCurrentVersionId) {
store.dispatch(
versionsActions.setCurrentVersionId({ versionId: headVersionId }),
);
}
};

const loadDiffAndRender = ({
Expand Down Expand Up @@ -379,6 +385,10 @@ describe(__filename, () => {
}),
);

store.dispatch(
versionsActions.setCurrentVersionId({ versionId: version.id }),
);

const dispatch = spyOn(store, 'dispatch');
const fakeThunk = createFakeThunk();
const _fetchVersionFile = fakeThunk.createThunk;
Expand All @@ -397,36 +407,45 @@ describe(__filename, () => {
});
});

it('does not dispatch fetchVersionFile() when a file is loading', () => {
it('dispatches an action to set current version id if currentVersionId is unset', () => {
const addonId = 9999;
const baseVersionId = 1;
const headVersionId = baseVersionId + 1;
const version = { ...fakeVersionWithDiff, id: headVersionId };
const store = configureStore();
_loadDiff({
addonId,
baseVersionId,
headVersionId,
store,
version,
setCurrentVersionId: false,
});
const dispatch = spyOn(store, 'dispatch');

store.dispatch(
versionsActions.loadVersionInfo({
version,
comparedToVersionId: baseVersionId,
}),
);

store.dispatch(
versionsActions.loadDiff({
addonId,
baseVersionId,
headVersionId,
version,
}),
);
render({
...getRouteParams({ addonId, baseVersionId, headVersionId }),
store,
});

store.dispatch(
versionsActions.beginFetchVersionFile({
path: version.file.selected_file,
versionId: version.id,
}),
expect(dispatch).toHaveBeenCalledWith(
versionsActions.setCurrentVersionId({ versionId: headVersionId }),
);
});

it('does not dispatch fetchVersionFile() when a file is loading', () => {
const addonId = 9999;
const baseVersionId = 1;
const headVersionId = baseVersionId + 1;
const version = { ...fakeVersionWithDiff, id: headVersionId };
const store = configureStore();
_loadDiff({
addonId,
baseVersionId,
headVersionId,
store,
version,
});
const dispatch = spyOn(store, 'dispatch');

render({
Expand Down
14 changes: 14 additions & 0 deletions src/pages/Compare/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getVersionFile,
getVersionInfo,
viewVersionFile,
actions as versionsActions,
} from '../../reducers/versions';
import {
createCodeLineAnchorGetter,
Expand Down Expand Up @@ -49,6 +50,7 @@ type PropsFromState = {
version: Version | void | null;
versionFile: VersionFile | void | null;
versionFileIsLoading: boolean;
currentVersionId: number | null | undefined | false;
};

type Props = RouteComponentProps<PropsFromRouter> &
Expand Down Expand Up @@ -76,6 +78,7 @@ export class CompareBase extends React.Component<Props> {
_fetchDiff,
_fetchVersionFile,
compareInfo,
currentVersionId,
dispatch,
history,
match,
Expand Down Expand Up @@ -112,6 +115,12 @@ export class CompareBase extends React.Component<Props> {
path: path || undefined,
}),
);
return;
}

if (version && !currentVersionId) {
dispatch(versionsActions.setCurrentVersionId({ versionId: version.id }));
return;
}

if (version && !versionFileIsLoading && versionFile === undefined) {
Expand Down Expand Up @@ -235,6 +244,10 @@ export const mapStateToProps = (
comparedToVersionId: baseVersionId,
});

const currentVersionId = version
? state.versions.currentVersionId
: undefined;

let versionFile;
if (version) {
versionFile = getVersionFile(
Expand All @@ -247,6 +260,7 @@ export const mapStateToProps = (
return {
addonId,
compareInfo,
currentVersionId,
path,
version,
versionFile,
Expand Down

0 comments on commit 9e7b293

Please sign in to comment.