From 68c59ec3efb37ee71e4ff6e245d81374941f807c Mon Sep 17 00:00:00 2001
From: Julien Bouquillon <julien.bouquillon@sg.social.gouv.fr>
Date: Tue, 8 Dec 2020 22:41:23 +0100
Subject: [PATCH] tests: add exportContributionAlerts snapshot

---
 .../exportContributionAlerts.test.js.snap     |   3 +
 .../__test__/exportContributionAlerts.test.js | 141 ++++++++++++++++++
 .../alert-cli/src/exportContributionAlerts.js |   4 +-
 3 files changed, 146 insertions(+), 2 deletions(-)
 create mode 100644 targets/alert-cli/src/__test__/__snapshots__/exportContributionAlerts.test.js.snap
 create mode 100644 targets/alert-cli/src/__test__/exportContributionAlerts.test.js

diff --git a/targets/alert-cli/src/__test__/__snapshots__/exportContributionAlerts.test.js.snap b/targets/alert-cli/src/__test__/__snapshots__/exportContributionAlerts.test.js.snap
new file mode 100644
index 000000000..c7a8dd5e0
--- /dev/null
+++ b/targets/alert-cli/src/__test__/__snapshots__/exportContributionAlerts.test.js.snap
@@ -0,0 +1,3 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`exportContributionAlerts should export changes to contributions API 1`] = `undefined`;
diff --git a/targets/alert-cli/src/__test__/exportContributionAlerts.test.js b/targets/alert-cli/src/__test__/exportContributionAlerts.test.js
new file mode 100644
index 000000000..950fdf729
--- /dev/null
+++ b/targets/alert-cli/src/__test__/exportContributionAlerts.test.js
@@ -0,0 +1,141 @@
+/* eslint-disable */
+jest.mock("node-fetch");
+
+import fetch from "node-fetch";
+
+import {
+  contribApiUrl,
+  exportContributionAlerts,
+} from "../exportContributionAlerts";
+
+describe("exportContributionAlerts", () => {
+  it("should export changes to contributions API", async () => {
+    const changes = [
+      {
+        type: "dila",
+        added: [
+          {
+            data: {
+              cid: 42,
+            },
+          },
+        ],
+        removed: [
+          {
+            data: {
+              cid: 55,
+            },
+          },
+        ],
+        modified: [
+          {
+            context: {
+              containerId: "LEGITEXT000006072050",
+            },
+            previous: {
+              data: {
+                etat: "VIGUEUR",
+                texte: "old text",
+                nota: "nota 2",
+              },
+            },
+            data: {
+              etat: "NON VIGUEUR",
+              cid: 45,
+              texte: "new text",
+              nota: "nota 1",
+            },
+          },
+        ],
+        documents: [
+          {
+            document: {
+              source: "contributions",
+            },
+            references: [
+              {
+                dila_cid: 42,
+                dila_id: 43,
+              },
+              {
+                dila_cid: 45,
+                dila_id: 46,
+              },
+            ],
+          },
+          {
+            document: {
+              source: "not-contributions",
+            },
+            references: [
+              {
+                x: 2021,
+                y: 2022,
+              },
+              {
+                x: 2023,
+                y: 2024,
+              },
+            ],
+          },
+        ],
+      },
+      {
+        type: "not-dila",
+        documents: [
+          {
+            document: {
+              source: "contributions",
+            },
+            references: [
+              {
+                dila_cid: 123,
+                dila_id: 456,
+              },
+              {
+                dila_cid: 789,
+                dila_id: 987,
+              },
+            ],
+          },
+          {
+            document: {
+              source: "not-contributions",
+            },
+            references: [
+              {
+                id: 99,
+                x: 66,
+              },
+            ],
+          },
+        ],
+      },
+    ];
+
+    expect(await exportContributionAlerts(changes)).toMatchSnapshot();
+    fetch.mockReturnValue(Promise.resolve());
+    expect(fetch).toHaveBeenCalledTimes(1);
+    expect(fetch).toHaveBeenCalledWith(contribApiUrl, {
+      method: "POST",
+      headers: {
+        "Content-Type": "application/json",
+      },
+      body: JSON.stringify([
+        { cid: 42, id: 43, value: { type: "added" } },
+        {
+          cid: 45,
+          id: 46,
+          value: {
+            etat: { current: "NON VIGUEUR", previous: "VIGUEUR" },
+            texts: [
+              { current: "new text", previous: "old text" },
+              { current: "nota 1", previous: "nota 2" },
+            ],
+            type: "modified",
+          },
+        },
+      ]),
+    });
+  });
+});
diff --git a/targets/alert-cli/src/exportContributionAlerts.js b/targets/alert-cli/src/exportContributionAlerts.js
index ca6fed13b..0c7bef3ce 100644
--- a/targets/alert-cli/src/exportContributionAlerts.js
+++ b/targets/alert-cli/src/exportContributionAlerts.js
@@ -1,6 +1,6 @@
 import fetch from "node-fetch";
 
-const contribApi =
+export const contribApiUrl =
   "https://contributions-api.codedutravail.fabrique.social.gouv.fr/alerts";
 
 /**
@@ -28,7 +28,7 @@ export async function exportContributionAlerts(changes) {
       }));
     });
   });
-  await fetch(contribApi, {
+  await fetch(contribApiUrl, {
     body: JSON.stringify(contributions),
     headers: {
       "Content-Type": "application/json",