Skip to content

Commit

Permalink
Fixed a bug with delete sections
Browse files Browse the repository at this point in the history
  • Loading branch information
SamGodwin2 committed Aug 5, 2019
1 parent 464bf92 commit 051b144
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion eq-author-api/schema/resolvers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const Resolvers = {
const section = find(ctx.questionnaire.sections, { id: input.id });
remove(ctx.questionnaire.sections, section);
onPageDeleted(ctx, input.id);
return section;
return ctx.questionnaire;
}),
moveSection: createMutation((_, { input }, ctx) => {
const removedSection = first(
Expand Down
2 changes: 1 addition & 1 deletion eq-author-api/schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ type Mutation {
duplicateQuestionnaire(input: DuplicateQuestionnaireInput!): Questionnaire
createSection(input: CreateSectionInput!): Section
updateSection(input: UpdateSectionInput!): Section
deleteSection(input: DeleteSectionInput!): Section
deleteSection(input: DeleteSectionInput!): Questionnaire
moveSection(input: MoveSectionInput!): Section
duplicateSection(input: DuplicateSectionInput!): Section
updatePage(input: UpdatePageInput!): Page
Expand Down
10 changes: 6 additions & 4 deletions eq-author/src/App/section/Design/withDeleteSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ const pluralize = (count, word, plural = word + "s") => {

export const handleDeletion = (
{ history, onAddSection, match: { params } },
questionnaire
{ data },
oldQuestionnaire
) => {
const questionnaire = data.deleteSection;
const { sectionId, questionnaireId } = params;

if (questionnaire.sections.length === 1) {
if (questionnaire.sections.length === 0) {
return onAddSection();
}

const nextSection = getNextSection(questionnaire.sections, sectionId);
const nextSection = getNextSection(oldQuestionnaire.sections, sectionId);
const nextPage = nextSection.pages[0];

history.push(
Expand Down Expand Up @@ -107,7 +109,7 @@ export const mapMutateToProps = ({ ownProps, mutate }) => ({
});

return mutation
.then(() => handleDeletion(ownProps, questionnaire))
.then(data => handleDeletion(ownProps, data, questionnaire))
.then(() => displayToast(ownProps, questionnaire))
.then(() => mutation);
},
Expand Down
16 changes: 9 additions & 7 deletions eq-author/src/App/section/Design/withDeleteSection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe("withDeleteSection", () => {

result = {
data: {
deleteSection: deletedPage,
deleteSection: {
id: "questionnaire",
sections: [],
},
},
};

Expand Down Expand Up @@ -152,19 +155,18 @@ describe("withDeleteSection", () => {

describe("handleDeletion", () => {
describe("when only one section in questionnaire", () => {
beforeEach(() => {
questionnaire.sections = [currentSection];
});

it("should add new section", () => {
handleDeletion(ownProps, questionnaire);
handleDeletion(ownProps, result, {});
expect(onAddSection).toHaveBeenCalled();
});
});

describe("when more than one section in questionnaire", () => {
it("should redirect to another section", () => {
handleDeletion(ownProps, questionnaire);
result.data.deleteSection.sections = [
{ id: "section 1", pages: [{ id: "page 1" }] },
];
handleDeletion(ownProps, result, questionnaire);
expect(history.push).toHaveBeenCalled();
});
});
Expand Down
8 changes: 4 additions & 4 deletions eq-author/src/graphql/deleteSection.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mutation DeleteSection($input: DeleteSectionInput!) {
deleteSection(input: $input) {
id
questionnaire {
sections {
id
questionnaireInfo {
totalSectionCount
}
}
questionnaireInfo {
totalSectionCount
}
}
}

0 comments on commit 051b144

Please sign in to comment.