<commit_before>import { mapMutateToProps, createUpdater } from "./withMovePage";
import { getLink } from "utils/UrlUtils";
import fragment from "graphql/fragments/movePage.graphql";

describe("withMovePage", () => {
  let ownProps, history, match, props, mutate, args, result;

  beforeEach(() => {
    match = {
      params: {
        questionnaireId: "1"
      }
    };

    history = {
      replace: jest.fn()
    };

    ownProps = {
      history,
      match
    };

    args = {
      from: {
        id: "1",
        sectionId: "1",
        position: 0
      },
      to: {
        id: "1",
        sectionId: "2",
        position: 1
      }
    };

    result = {
      data: {
        movePage: {
          id: args.to.id,
          section: {
            id: args.to.sectionId,
            __typename: "Section"
          },
          position: args.to.position,
          __typename: "QuestionPage"
        }
      }
    };
  });

  describe("mapMutateToProps", () => {
    beforeEach(() => {
      mutate = jest.fn(() => Promise.resolve(result));
      props = mapMutateToProps({ ownProps, mutate });
    });

    it("supplies an onMovePage prop", () => {
      expect(props.onMovePage).toBeInstanceOf(Function);
    });

    describe("onMovePage", () => {
      it("provides the necessary arguments to mutate", () => {
        const expected = {
          variables: {
            input: args.to
          },
          optimisticResponse: result.data,
          update: expect.any(Function)
        };

        return props.onMovePage(args).then(() => {
          expect(mutate).toHaveBeenCalledWith(expected);
        });
      });

      it("should return promise that resolves to movePage result", () => {
        return expect(props.onMovePage(args)).resolves.toBe(result);
      });

      it("should redirect if the section id has changed", () => {
        const expected = getLink(
          match.params.questionnaireId,
          args.to.sectionId,
          args.to.id
        );

        return props.onMovePage(args).then(() => {
          expect(history.replace).toHaveBeenCalledWith(expected);
        });
      });
    });
  });

  describe("createUpdate", () => {
    let proxy, fromSection, toSection, page;

    beforeEach(() => {
      page = { id: args.from.id, position: 0 };

      fromSection = {
        id: args.from.sectionId,
        pages: [page, { id: "2", position: 1 }]
      };

      toSection = {
        id: args.to.sectionId,
        pages: [{ id: "3", position: 0 }]
      };

      proxy = {
        writeFragment: jest.fn(),
        readFragment: jest.fn()
      };

      proxy.readFragment
        .mockReturnValueOnce(fromSection)
        .mockReturnValueOnce(toSection);
    });

    it("should update the cache correctly", () => {
      const updater = createUpdater(args);
      updater(proxy, result);

      expect(proxy.writeFragment).toHaveBeenCalledWith({
        id: `Section${args.from.sectionId}`,
        fragment,
        data: fromSection
      });

      expect(proxy.writeFragment).toHaveBeenCalledWith({
        id: `Section${args.to.sectionId}`,
        fragment,
        data: toSection
      });

      expect(fromSection.pages).not.toContain(page);
      expect(toSection.pages[args.to.position]).toBe(page);
    });
  });
});<commit_msg>add failing test for move page bug<commit_after>import { mapMutateToProps, createUpdater } from "./withMovePage";
import { getLink } from "utils/UrlUtils";
import fragment from "graphql/fragments/movePage.graphql";

describe("withMovePage", () => {
  let ownProps, history, match, props, mutate, args, result;

  beforeEach(() => {
    match = {
      params: {
        questionnaireId: "1"
      }
    };

    history = {
      replace: jest.fn()
    };

    ownProps = {
      history,
      match
    };

    args = {
      from: {
        id: "1",
        sectionId: "1",
        position: 0
      },
      to: {
        id: "1",
        sectionId: "2",
        position: 1
      }
    };

    result = {
      data: {
        movePage: {
          id: args.to.id,
          section: {
            id: args.to.sectionId,
            __typename: "Section"
          },
          position: args.to.position,
          __typename: "QuestionPage"
        }
      }
    };
  });

  describe("mapMutateToProps", () => {
    beforeEach(() => {
      mutate = jest.fn(() => Promise.resolve(result));
      props = mapMutateToProps({ ownProps, mutate });
    });

    it("supplies an onMovePage prop", () => {
      expect(props.onMovePage).toBeInstanceOf(Function);
    });

    describe("onMovePage", () => {
      it("provides the necessary arguments to mutate", () => {
        const expected = {
          variables: {
            input: args.to
          },
          optimisticResponse: result.data,
          update: expect.any(Function)
        };

        return props.onMovePage(args).then(() => {
          expect(mutate).toHaveBeenCalledWith(expected);
        });
      });

      it("should return promise that resolves to movePage result", () => {
        return expect(props.onMovePage(args)).resolves.toBe(result);
      });

      it("should redirect if the section id has changed", () => {
        const expected = getLink(
          match.params.questionnaireId,
          args.to.sectionId,
          args.to.id
        );

        return props.onMovePage(args).then(() => {
          expect(history.replace).toHaveBeenCalledWith(expected);
        });
      });
    });
  });

  describe("createUpdater", () => {
    let proxy, fromSection, toSection, page;

    beforeEach(() => {
      page = { id: args.from.id, position: 0 };

      fromSection = {
        id: args.from.sectionId,
        pages: [page, { id: "2", position: 1 }]
      };

      toSection = {
        id: args.to.sectionId,
        pages: [{ id: "3", position: 0 }]
      };

      proxy = {
        writeFragment: jest.fn(),
        readFragment: jest.fn()
      };

      proxy.readFragment
        .mockReturnValueOnce(fromSection)
        .mockReturnValueOnce(toSection);
    });

    it("should update the cache correctly", () => {
      const updater = createUpdater(args);
      updater(proxy, result);

      expect(proxy.writeFragment).toHaveBeenCalledWith({
        id: `Section${args.from.sectionId}`,
        fragment,
        data: fromSection
      });

      expect(proxy.writeFragment).toHaveBeenCalledWith({
        id: `Section${args.to.sectionId}`,
        fragment,
        data: toSection
      });

      expect(fromSection.pages).not.toContain(page);
      expect(toSection.pages[args.to.position]).toMatchObject({
        id: page.id,
        position: args.to.position
      });
    });

    it("should correctly update position values for all pages in a section", () => {
      const sections = {
        Section1: {
          id: args.from.sectionId,
          pages: [
            { id: "1", position: 0 },
            { id: "2", position: 1 },
            { id: "3", position: 2 }
          ]
        }
      };

      proxy = {
        writeFragment: jest.fn(({ id, data }) => {
          sections[id] = data;
        }),
        readFragment: jest.fn(({ id }) => {
          return sections[id];
        })
      };

      let updater = createUpdater({
        from: {
          id: "3",
          sectionId: "1",
          position: 2
        },
        to: {
          id: "3",
          sectionId: "1",
          position: 1
        }
      });

      updater(proxy, {
        data: {
          movePage: {
            id: "1",
            position: 1,
            __typename: "QuestionPage",
            section: {
              id: "1",
              __typename: "Section"
            }
          }
        }
      });

      updater = createUpdater({
        from: {
          id: "2",
          sectionId: "1",
          position: 1
        },
        to: {
          id: "2",
          sectionId: "1",
          position: 0
        }
      });

      updater(proxy, {
        data: {
          movePage: {
            id: "2",
            position: 0,
            __typename: "QuestionPage",
            section: {
              id: "1",
              __typename: "Section"
            }
          }
        }
      });

      expect(sections.Section1.pages).toEqual([
        { id: "2", position: 0 },
        { id: "1", position: 1 },
        { id: "3", position: 2 }
      ]);
    });
  });
});
{'fraction_non_alphanumeric': 0.14627502072722967, 'fraction_numerical': 0.006632713490465474, 'mean_word_length': 1.6704617330803289, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883260', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2482, 'n_words': 491}