| 1 | {WorkspaceView} = require 'atom' |
| 2 | |
| 3 | describe "pretty json", -> |
| 4 | [editor, editorView] = [] |
| 5 | |
| 6 | prettify = (callback) -> |
| 7 | editorView.trigger "pretty-json:prettify" |
| 8 | runs(callback) |
| 9 | |
| 10 | minify = (callback) -> |
| 11 | editorView.trigger "pretty-json:minify" |
| 12 | runs(callback) |
| 13 | |
| 14 | sortedPrettify = (callback) -> |
| 15 | editorView.trigger "pretty-json:sort-and-prettify" |
| 16 | runs(callback) |
| 17 | |
| 18 | beforeEach -> |
| 19 | waitsForPromise -> atom.packages.activatePackage('pretty-json') |
| 20 | waitsForPromise -> atom.packages.activatePackage('language-json') |
| 21 | |
| 22 | atom.workspaceView = new WorkspaceView |
| 23 | atom.workspaceView.openSync() |
| 24 | |
| 25 | editorView = atom.workspaceView.getActiveView() |
| 26 | editor = editorView.getEditor() |
| 27 | |
| 28 | describe "when no text is selected", -> |
| 29 | it "doesn't change anything", -> |
| 30 | editor.setText """ |
| 31 | Start |
| 32 | { "a": "b", "c": "d" } |
| 33 | End |
| 34 | """ |
| 35 | |
| 36 | prettify -> |
| 37 | expect(editor.getText()).toBe """ |
| 38 | Start |
| 39 | { "a": "b", "c": "d" } |
| 40 | End |
| 41 | """ |
| 42 | |
| 43 | describe "when a valid json text is selected", -> |
| 44 | it "formats it correctly", -> |
| 45 | editor.setText """ |
| 46 | Start |
| 47 | { "a": "b", "c": "d" } |
| 48 | End |
| 49 | """ |
| 50 | editor.setSelectedBufferRange([[1,0], [1, 22]]) |
| 51 | |
| 52 | prettify -> |
| 53 | expect(editor.getText()).toBe """ |
| 54 | Start |
| 55 | { |
| 56 | "a": "b", |
| 57 | "c": "d" |
| 58 | } |
| 59 | End |
| 60 | """ |
| 61 | |
| 62 | describe "when an invalid json text is selected", -> |
| 63 | it "doesn't change anything", -> |
| 64 | editor.setText """ |
| 65 | Start |
| 66 | {] |
| 67 | End |
| 68 | """ |
| 69 | editor.setSelectedBufferRange([[1,0], [1, 2]]) |
| 70 | |
| 71 | prettify -> |
| 72 | expect(editor.getText()).toBe """ |
| 73 | Start |
| 74 | {] |
| 75 | End |
| 76 | """ |
| 77 | |
| 78 | describe "JSON file", -> |
| 79 | beforeEach -> |
| 80 | editor.setGrammar(atom.syntax.selectGrammar('test.json')) |
| 81 | |
| 82 | describe "with invalid JSON", -> |
| 83 | it "doesn't change anything", -> |
| 84 | editor.setText """ |
| 85 | {] |
| 86 | """ |
| 87 | |
| 88 | prettify -> |
| 89 | expect(editor.getText()).toBe """ |
| 90 | {] |
| 91 | """ |
| 92 | |
| 93 | describe "with valid JSON", -> |
| 94 | it "formats the whole file correctly", -> |
| 95 | editor.setText """ |
| 96 | { "a": "b", "c": "d" } |
| 97 | """ |
| 98 | |
| 99 | prettify -> |
| 100 | expect(editor.getText()).toBe """ |
| 101 | { |
| 102 | "a": "b", |
| 103 | "c": "d" |
| 104 | } |
| 105 | """ |
| 106 | |
| 107 | describe "Sort and prettify", -> |
| 108 | beforeEach -> |
| 109 | editor.setGrammar(atom.syntax.selectGrammar('test.json')) |
| 110 | |
| 111 | describe "with invalid JSON", -> |
| 112 | it "doesn't change anything", -> |
| 113 | editor.setText """ |
| 114 | {] |
| 115 | """ |
| 116 | |
| 117 | sortedPrettify -> |
| 118 | expect(editor.getText()).toBe """ |
| 119 | {] |
| 120 | """ |
| 121 | |
| 122 | describe "with valid JSON", -> |
| 123 | it "formats the whole file correctly", -> |
| 124 | editor.setText """ |
| 125 | { "c": "d", "a": "b" } |
| 126 | """ |
| 127 | |
| 128 | sortedPrettify -> |
| 129 | expect(editor.getText()).toBe """ |
| 130 | { |
| 131 | "a": "b", |
| 132 | "c": "d" |
| 133 | } |
| 134 | """ |
| 135 | |
| 136 | describe "Minify JSON file", -> |
| 137 | beforeEach -> |
| 138 | editor.setGrammar(atom.syntax.selectGrammar('test.json')) |
| 139 | |
| 140 | it "Returns same string from invalid JSON", -> |
| 141 | editor.setText """ |
| 142 | { |
| 143 | [ |
| 144 | } |
| 145 | """ |
| 146 | |
| 147 | minify -> |
| 148 | expect(editor.getText()).toBe """ |
| 149 | { |
| 150 | [ |
| 151 | } |
| 152 | """ |
| 153 | |
| 154 | it "Minifies valid JSON", -> |
| 155 | editor.setText """ |
| 156 | { |
| 157 | "a": "b", |
| 158 | "c": "d", |
| 159 | "num": 123 |
| 160 | } |
| 161 | """ |
| 162 | |
| 163 | minify -> |
| 164 | expect(editor.getText()).toBe """ |
| 165 | {"a":"b","c":"d","num":123} |
| 166 | """ |
| 167 | |
| 168 | describe "Minify selected JSON", -> |
| 169 | it "Minifies JSON data", -> |
| 170 | editor.setText """ |
| 171 | Start |
| 172 | { |
| 173 | "a": "b", |
| 174 | "c": "d", |
| 175 | "num": 123 |
| 176 | } |
| 177 | End |
| 178 | """ |
| 179 | editor.setSelectedBufferRange([[1,0], [5, 1]]) |
| 180 | |
| 181 | minify -> |
| 182 | expect(editor.getText()).toBe """ |
| 183 | Start |
| 184 | {"a":"b","c":"d","num":123} |
| 185 | End |
| 186 | """ |