Skip to content

Commit

Permalink
tests: add exportContributionAlerts snapshot (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bouquillon authored Dec 9, 2020
1 parent 47fe826 commit 2b13c07
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`exportContributionAlerts should export changes to contributions API 1`] = `undefined`;
141 changes: 141 additions & 0 deletions targets/alert-cli/src/__test__/exportContributionAlerts.test.js
Original file line number Diff line number Diff line change
@@ -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",
},
},
]),
});
});
});
4 changes: 2 additions & 2 deletions targets/alert-cli/src/exportContributionAlerts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from "node-fetch";

const contribApi =
export const contribApiUrl =
"https://contributions-api.codedutravail.fabrique.social.gouv.fr/alerts";

/**
Expand Down Expand Up @@ -28,7 +28,7 @@ export async function exportContributionAlerts(changes) {
}));
});
});
await fetch(contribApi, {
await fetch(contribApiUrl, {
body: JSON.stringify(contributions),
headers: {
"Content-Type": "application/json",
Expand Down

0 comments on commit 2b13c07

Please sign in to comment.