2020-04-20 04:58:42 +00:00
|
|
|
/* eslint-disable flowtype/require-valid-file-annotation */
|
2020-06-20 20:59:15 +00:00
|
|
|
import { flushdb } from "../test/support";
|
|
|
|
import BacklinksService from "./backlinks";
|
|
|
|
import { buildDocument } from "../test/factories";
|
|
|
|
import Backlink from "../models/Backlink";
|
2020-04-20 04:58:42 +00:00
|
|
|
|
|
|
|
const Backlinks = new BacklinksService();
|
|
|
|
|
|
|
|
beforeEach(flushdb);
|
|
|
|
beforeEach(jest.resetAllMocks);
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
describe("documents.update", () => {
|
|
|
|
test("should not fail on a document with no previous revisions", async () => {
|
2020-04-24 12:29:48 +00:00
|
|
|
const otherDocument = await buildDocument();
|
|
|
|
const document = await buildDocument({
|
|
|
|
text: `[this is a link](${otherDocument.url})`,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2020-04-24 12:29:48 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: document.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
const backlinks = await Backlink.findAll({
|
|
|
|
where: { reverseDocumentId: document.id },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(backlinks.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
test("should create new backlink records", async () => {
|
2020-04-20 04:58:42 +00:00
|
|
|
const otherDocument = await buildDocument();
|
|
|
|
const document = await buildDocument();
|
|
|
|
|
|
|
|
document.text = `[this is a link](${otherDocument.url})`;
|
|
|
|
await document.save();
|
|
|
|
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2020-04-20 04:58:42 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: document.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
const backlinks = await Backlink.findAll({
|
|
|
|
where: { reverseDocumentId: document.id },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(backlinks.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
test("should destroy removed backlink records", async () => {
|
2020-04-20 04:58:42 +00:00
|
|
|
const otherDocument = await buildDocument();
|
|
|
|
const document = await buildDocument({
|
|
|
|
text: `[this is a link](${otherDocument.url})`,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.publish",
|
2020-04-20 04:58:42 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: document.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
document.text = "Link is gone";
|
2020-04-20 04:58:42 +00:00
|
|
|
await document.save();
|
|
|
|
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2020-04-20 04:58:42 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: document.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
const backlinks = await Backlink.findAll({
|
|
|
|
where: { reverseDocumentId: document.id },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(backlinks.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
test("should update titles in backlinked documents", async () => {
|
|
|
|
const newTitle = "test";
|
2020-04-20 04:58:42 +00:00
|
|
|
const document = await buildDocument();
|
|
|
|
const otherDocument = await buildDocument();
|
|
|
|
|
|
|
|
// create a doc with a link back
|
|
|
|
document.text = `[${otherDocument.title}](${otherDocument.url})`;
|
|
|
|
await document.save();
|
|
|
|
|
|
|
|
// ensure the backlinks are created
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2020-04-20 04:58:42 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: document.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
// change the title of the linked doc
|
|
|
|
otherDocument.title = newTitle;
|
|
|
|
await otherDocument.save();
|
|
|
|
|
|
|
|
// does the text get updated with the new title
|
|
|
|
await Backlinks.on({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2020-04-20 04:58:42 +00:00
|
|
|
documentId: otherDocument.id,
|
|
|
|
collectionId: otherDocument.collectionId,
|
|
|
|
teamId: otherDocument.teamId,
|
|
|
|
actorId: otherDocument.createdById,
|
|
|
|
data: { autosave: false },
|
|
|
|
});
|
|
|
|
await document.reload();
|
|
|
|
|
|
|
|
expect(document.text).toBe(`[${newTitle}](${otherDocument.url})`);
|
|
|
|
});
|
|
|
|
});
|