|
| 1 | +import { shallow } from "enzyme"; |
| 2 | +import React from "react"; |
| 3 | + |
| 4 | +import Ansi from "../src/index"; |
| 5 | + |
| 6 | +const GREEN_FG = "\u001b[32m"; |
| 7 | +const YELLOW_BG = "\u001b[43m"; |
| 8 | +const BOLD = "\u001b[1m"; |
| 9 | +const RESET = "\u001b[0;m"; |
| 10 | + |
| 11 | +describe("Ansi", () => { |
| 12 | + test("hello world", () => { |
| 13 | + const el = shallow(React.createElement(Ansi, null, "hello world")); |
| 14 | + expect(el).not.toBeNull(); |
| 15 | + expect(el.text()).toBe("hello world"); |
| 16 | + }); |
| 17 | + |
| 18 | + test("can color", () => { |
| 19 | + const el = shallow( |
| 20 | + React.createElement(Ansi, null, `hello ${GREEN_FG}world`) |
| 21 | + ); |
| 22 | + expect(el).not.toBeNull(); |
| 23 | + expect(el.text()).toBe("hello world"); |
| 24 | + expect(el.html()).toBe( |
| 25 | + '<code><span>hello </span><span style="color:rgb(0, 187, 0)">world</span></code>' |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + test("can have className", () => { |
| 30 | + const el = shallow( |
| 31 | + React.createElement(Ansi, { className: "my-class" }, "hello world") |
| 32 | + ); |
| 33 | + expect(el).not.toBeNull(); |
| 34 | + expect(el.text()).toBe("hello world"); |
| 35 | + expect(el.html()).toBe( |
| 36 | + '<code class="my-class"><span>hello world</span></code>' |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + test("can nest", () => { |
| 41 | + const el = shallow( |
| 42 | + React.createElement( |
| 43 | + Ansi, |
| 44 | + null, |
| 45 | + `hello ${GREEN_FG}wo${YELLOW_BG}rl${RESET}d` |
| 46 | + ) |
| 47 | + ); |
| 48 | + expect(el).not.toBeNull(); |
| 49 | + expect(el.text()).toBe("hello world"); |
| 50 | + expect(el.html()).toBe( |
| 51 | + '<code><span>hello </span><span style="color:rgb(0, 187, 0)">wo</span><span style="background-color:rgb(187, 187, 0);color:rgb(0, 187, 0)">rl</span><span>d</span></code>' |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + test("can handle carriage symbol", () => { |
| 56 | + const el = shallow( |
| 57 | + React.createElement( |
| 58 | + Ansi, |
| 59 | + null, |
| 60 | + "this sentence\rthat\nwill make you pause" |
| 61 | + ) |
| 62 | + ); |
| 63 | + expect(el).not.toBeNull(); |
| 64 | + expect(el.text()).toBe("that sentence\nwill make you pause"); |
| 65 | + }); |
| 66 | + |
| 67 | + test("can linkify", () => { |
| 68 | + const el = shallow( |
| 69 | + React.createElement( |
| 70 | + Ansi, |
| 71 | + { linkify: true }, |
| 72 | + "this is a link: https://nteract.io/" |
| 73 | + ) |
| 74 | + ); |
| 75 | + expect(el).not.toBeNull(); |
| 76 | + expect(el.text()).toBe("this is a link: https://nteract.io/"); |
| 77 | + expect(el.html()).toBe( |
| 78 | + '<code><span>this is a link: <a href="https://nteract.io/" target="_blank">https://nteract.io/</a></span></code>' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + test("can linkify links starting with www.", () => { |
| 83 | + const el = shallow( |
| 84 | + React.createElement( |
| 85 | + Ansi, |
| 86 | + { linkify: true }, |
| 87 | + "this is a link: www.google.com" |
| 88 | + ) |
| 89 | + ); |
| 90 | + expect(el).not.toBeNull(); |
| 91 | + expect(el.text()).toBe("this is a link: www.google.com"); |
| 92 | + expect(el.html()).toBe( |
| 93 | + '<code><span>this is a link: <a href="http://www.google.com" target="_blank">www.google.com</a></span></code>' |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + test("doesn't linkify partial matches", () => { |
| 98 | + const el = shallow( |
| 99 | + React.createElement( |
| 100 | + Ansi, |
| 101 | + { linkify: true }, |
| 102 | + "cant click this link: 'http://www.google.com'" |
| 103 | + ) |
| 104 | + ); |
| 105 | + expect(el).not.toBeNull(); |
| 106 | + expect(el.text()).toBe("cant click this link: 'http://www.google.com'"); |
| 107 | + expect(el.html()).toBe( |
| 108 | + "<code><span>cant click this link: 'http://www.google.com'</span></code>" |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + test("can distinguish URL-ish text", () => { |
| 113 | + const el = shallow( |
| 114 | + React.createElement( |
| 115 | + Ansi, |
| 116 | + { linkify: true }, |
| 117 | + "<transport.model.TransportInfo" |
| 118 | + ) |
| 119 | + ); |
| 120 | + expect(el).not.toBeNull(); |
| 121 | + expect(el.text()).toBe("<transport.model.TransportInfo"); |
| 122 | + }); |
| 123 | + |
| 124 | + test("can distinguish URL-ish text", () => { |
| 125 | + const el = shallow( |
| 126 | + React.createElement( |
| 127 | + Ansi, |
| 128 | + { linkify: true }, |
| 129 | + "<module 'something' from '/usr/local/lib/python2.7/dist-packages/something/__init__.pyc'>" |
| 130 | + ) |
| 131 | + ); |
| 132 | + expect(el).not.toBeNull(); |
| 133 | + expect(el.text()).toBe( |
| 134 | + "<module 'something' from '/usr/local/lib/python2.7/dist-packages/something/__init__.pyc'>" |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + describe("useClasses options", () => { |
| 139 | + test("can add the font color class", () => { |
| 140 | + const el = shallow( |
| 141 | + React.createElement( |
| 142 | + Ansi, |
| 143 | + { useClasses: true }, |
| 144 | + `hello ${GREEN_FG}world` |
| 145 | + ) |
| 146 | + ); |
| 147 | + expect(el).not.toBeNull(); |
| 148 | + expect(el.text()).toBe("hello world"); |
| 149 | + expect(el.html()).toBe( |
| 150 | + '<code><span>hello </span><span class="ansi-green-fg">world</span></code>' |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + test("can add the background color class", () => { |
| 155 | + const el = shallow( |
| 156 | + React.createElement( |
| 157 | + Ansi, |
| 158 | + { useClasses: true }, |
| 159 | + `hello ${YELLOW_BG}world` |
| 160 | + ) |
| 161 | + ); |
| 162 | + expect(el).not.toBeNull(); |
| 163 | + expect(el.text()).toBe("hello world"); |
| 164 | + expect(el.html()).toBe( |
| 165 | + '<code><span>hello </span><span class="ansi-yellow-bg">world</span></code>' |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + test("can add font and background color classes", () => { |
| 170 | + const el = shallow( |
| 171 | + React.createElement( |
| 172 | + Ansi, |
| 173 | + { useClasses: true }, |
| 174 | + `hello ${GREEN_FG}${YELLOW_BG}world` |
| 175 | + ) |
| 176 | + ); |
| 177 | + expect(el).not.toBeNull(); |
| 178 | + expect(el.text()).toBe("hello world"); |
| 179 | + expect(el.html()).toBe( |
| 180 | + '<code><span>hello </span><span class="ansi-yellow-bg ansi-green-fg">world</span></code>' |
| 181 | + ); |
| 182 | + }); |
| 183 | + |
| 184 | + test("can add text decoration classes", () => { |
| 185 | + const el = shallow( |
| 186 | + React.createElement( |
| 187 | + Ansi, |
| 188 | + { useClasses: true }, |
| 189 | + `hello ${GREEN_FG}${BOLD}world${RESET}!` |
| 190 | + ) |
| 191 | + ); |
| 192 | + expect(el).not.toBeNull(); |
| 193 | + expect(el.text()).toBe("hello world!"); |
| 194 | + expect(el.html()).toBe( |
| 195 | + '<code><span>hello </span><span class="ansi-green-fg ansi-bold">world</span><span>!</span></code>' |
| 196 | + ); |
| 197 | + }); |
| 198 | + |
| 199 | + test("can use useClasses with linkify", () => { |
| 200 | + const el = shallow( |
| 201 | + React.createElement( |
| 202 | + Ansi, |
| 203 | + { linkify: true, useClasses: true }, |
| 204 | + `${GREEN_FG}this is a link: https://nteract.io/` |
| 205 | + ) |
| 206 | + ); |
| 207 | + expect(el).not.toBeNull(); |
| 208 | + expect(el.text()).toBe("this is a link: https://nteract.io/"); |
| 209 | + expect(el.html()).toBe( |
| 210 | + '<code><span class="ansi-green-fg">this is a link: <a href="https://nteract.io/" target="_blank">https://nteract.io/</a></span></code>' |
| 211 | + ); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments