Skip to content

Commit

Permalink
Add more tests remove utility
Browse files Browse the repository at this point in the history
- Add more tests for cell attachment corner cases
- Remove blob utility method
- Remove mock for URL.createObjectUrl
  • Loading branch information
bgparkerdev committed Jun 26, 2020
1 parent 401454c commit c9442fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 75 deletions.
56 changes: 34 additions & 22 deletions __tests__/attachment.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as React from "react";
import { mount } from "enzyme";

import { toAttachments } from "../src/attachment/attachment";
import { Attachments } from "../src/attachment/attachment-transformer";
import MarkdownRender from "../src";

function buildSource(attachment?: string): string {
Expand All @@ -19,42 +17,56 @@ function buildSource(attachment?: string): string {
`;
}

const inlineAttachment = "![image.png](attachment:spot-the-difference-2a.jpg)";

const cellAttachments = {
"spot-the-difference-2a.jpg": {
"image/jpeg":
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4",
},
"someotherimage.png": {
"image/png": "someothercontent",
},
"spot-the-difference-2a.jpg":
"data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4",
"someotherimage.png": "data:image/png;base64,someothercontent",
};

const attchmnts: Attachments = toAttachments(cellAttachments);
const inlineAttachment = "![image.png](attachment:spot-the-difference-2a.jpg)";

//uses value 'mocked' defined in test-setup.js for return of URL.createObjectURL. Jest can't execute logic that uses this method.
describe("attachment", () => {
test("toAttachments properly converts jupyter nb format cell attachments to markdown AST based attachments", () => {
expect(attchmnts["spot-the-difference-2a.jpg"]).toEqual("mocked");
expect(attchmnts["someotherimage.png"]).toEqual("mocked");
test("Attachment should be added to source if the source contains a valid jupyter attachment reference", () => {
const wrapped = mount(
<MarkdownRender
source={buildSource(inlineAttachment)}
attachments={cellAttachments}
></MarkdownRender>
);
const imgProps = wrapped.find("img").props();
expect(imgProps).toHaveProperty(
"src",
"data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4"
);
});

test("Attachments should be added to source if there is matching reference", () => {
test("Source is unaffected when it contains jupyter attachment reference and cell attachments is not provided", () => {
const wrapped = mount(
<MarkdownRender source={buildSource(inlineAttachment)}></MarkdownRender>
);
const imgProps = wrapped.find("img").props();
expect(imgProps).toHaveProperty(
"src",
"attachment:spot-the-difference-2a.jpg"
);
});

test("Source is unaffected when empty attachment is provided", () => {
const wrapped = mount(
<MarkdownRender
source={buildSource(inlineAttachment)}
attachments={attchmnts}
source={buildSource("(![someimg](bogus.jpg)")}
attachments={{}}
></MarkdownRender>
);
const imgProps = wrapped.find("img").props();
expect(imgProps).toHaveProperty("src", "mocked");
expect(imgProps).toHaveProperty("src", "bogus.jpg");
});

test("Images should be unaffected if they are not juptyer attachments", () => {
test("Source is unaffected when it contains no jupyter attachment reference and cell attachments are provided", () => {
const wrapped = mount(
<MarkdownRender
source={buildSource("(![img](bogus.jpg)")}
source={buildSource("(![someimg](bogus.jpg)")}
attachments={cellAttachments}
></MarkdownRender>
);
const imgProps = wrapped.find("img").props();
Expand Down
49 changes: 0 additions & 49 deletions src/attachment/attachment.ts

This file was deleted.

5 changes: 1 addition & 4 deletions test-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ const Adapter = require("enzyme-adapter-react-16");

enzyme.configure({
adapter: new Adapter()
});
window.URL.createObjectURL = function () {
return "mocked"
}; //jest needs a mock for this, or tests that call this method won't work.
});

0 comments on commit c9442fa

Please sign in to comment.