|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { dedent, dedentString } from '../dedent'; |
| 5 | + |
| 6 | +describe('dedentString', () => { |
| 7 | + it('removes indentation in typical usage', () => { |
| 8 | + const output = dedentString(` |
| 9 | + type Query { |
| 10 | + me: User |
| 11 | + } |
| 12 | +
|
| 13 | + type User { |
| 14 | + id: ID |
| 15 | + name: String |
| 16 | + } |
| 17 | + `); |
| 18 | + expect(output).to.equal( |
| 19 | + [ |
| 20 | + 'type Query {', |
| 21 | + ' me: User', |
| 22 | + '}', |
| 23 | + '', |
| 24 | + 'type User {', |
| 25 | + ' id: ID', |
| 26 | + ' name: String', |
| 27 | + '}', |
| 28 | + ].join('\n'), |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('removes only the first level of indentation', () => { |
| 33 | + const output = dedentString(` |
| 34 | + first |
| 35 | + second |
| 36 | + third |
| 37 | + fourth |
| 38 | + `); |
| 39 | + expect(output).to.equal( |
| 40 | + ['first', ' second', ' third', ' fourth'].join('\n'), |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('does not escape special characters', () => { |
| 45 | + const output = dedentString(` |
| 46 | + type Root { |
| 47 | + field(arg: String = "wi\th de\fault"): String |
| 48 | + } |
| 49 | + `); |
| 50 | + expect(output).to.equal( |
| 51 | + [ |
| 52 | + 'type Root {', |
| 53 | + ' field(arg: String = "wi\th de\fault"): String', |
| 54 | + '}', |
| 55 | + ].join('\n'), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('also removes indentation using tabs', () => { |
| 60 | + const output = dedentString(` |
| 61 | + \t\t type Query { |
| 62 | + \t\t me: User |
| 63 | + \t\t } |
| 64 | + `); |
| 65 | + expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n')); |
| 66 | + }); |
| 67 | + |
| 68 | + it('removes leading and trailing newlines', () => { |
| 69 | + const output = dedentString(` |
| 70 | +
|
| 71 | +
|
| 72 | + type Query { |
| 73 | + me: User |
| 74 | + } |
| 75 | +
|
| 76 | +
|
| 77 | + `); |
| 78 | + expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n')); |
| 79 | + }); |
| 80 | + |
| 81 | + it('removes all trailing spaces and tabs', () => { |
| 82 | + const output = dedentString(` |
| 83 | + type Query { |
| 84 | + me: User |
| 85 | + } |
| 86 | + \t\t \t `); |
| 87 | + expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n')); |
| 88 | + }); |
| 89 | + |
| 90 | + it('works on text without leading newline', () => { |
| 91 | + const output = dedentString(` type Query { |
| 92 | + me: User |
| 93 | + } |
| 94 | + `); |
| 95 | + expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n')); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('dedent', () => { |
| 100 | + it('removes indentation in typical usage', () => { |
| 101 | + const output = dedent` |
| 102 | + type Query { |
| 103 | + me: User |
| 104 | + } |
| 105 | + `; |
| 106 | + expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n')); |
| 107 | + }); |
| 108 | + |
| 109 | + it('supports expression interpolation', () => { |
| 110 | + const name = 'John'; |
| 111 | + const surname = 'Doe'; |
| 112 | + const output = dedent` |
| 113 | + { |
| 114 | + "me": { |
| 115 | + "name": "${name}", |
| 116 | + "surname": "${surname}" |
| 117 | + } |
| 118 | + } |
| 119 | + `; |
| 120 | + expect(output).to.equal( |
| 121 | + [ |
| 122 | + '{', |
| 123 | + ' "me": {', |
| 124 | + ' "name": "John",', |
| 125 | + ' "surname": "Doe"', |
| 126 | + ' }', |
| 127 | + '}', |
| 128 | + ].join('\n'), |
| 129 | + ); |
| 130 | + }); |
| 131 | +}); |
0 commit comments