|
| 1 | +import outdent from 'outdent'; |
| 2 | +import ts from 'typescript'; |
| 3 | +import { |
| 4 | + isDescendantOfXStateImport, |
| 5 | + isMachineCallExpression, |
| 6 | + isMachineNamedImportSpecifier, |
| 7 | + isMachinePropertyAccessExpression, |
| 8 | + isXStateImportClause, |
| 9 | + isXStateImportDeclaration, |
| 10 | +} from './predicates'; |
| 11 | +import { parse } from './test-utils'; |
| 12 | +import { findDescendant } from './utils'; |
| 13 | + |
| 14 | +describe('isXStateImportDeclaration', () => { |
| 15 | + test.each<[code: string, expected: boolean]>([ |
| 16 | + [`import xstate from 'xstate'`, true], |
| 17 | + [`import { Machine } from 'xstate'`, true], |
| 18 | + [`import { Machine as M } from 'xstate'`, true], |
| 19 | + [`import xstate from 'not-xstate'`, false], |
| 20 | + [`import { Machine } from 'not-xstate'`, false], |
| 21 | + [`import { Machine as M } from 'not-xstate'`, false], |
| 22 | + [`console.log('not an ImportDeclaration')`, false], |
| 23 | + ])('%s (%s)', (code, expected) => { |
| 24 | + const sourceFile = parse(code); |
| 25 | + const node = findDescendant(sourceFile, isXStateImportDeclaration); |
| 26 | + |
| 27 | + if (expected) { |
| 28 | + expect(node).not.toBeUndefined(); |
| 29 | + expect(node!.kind).toBe(ts.SyntaxKind.ImportDeclaration); |
| 30 | + } else { |
| 31 | + expect(node).toBeUndefined(); |
| 32 | + } |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('isDescendantOfXStateImport', () => { |
| 37 | + test.each<[code: string, expected: boolean]>([ |
| 38 | + [`import xstate from 'xstate'`, true], |
| 39 | + [`import { Machine } from 'xstate'`, true], |
| 40 | + [`import { Machine as M } from 'xstate'`, true], |
| 41 | + [`import xstate from 'not-xstate'`, false], |
| 42 | + [`import { Machine } from 'not-xstate'`, false], |
| 43 | + [`import { Machine as M } from 'not-xstate'`, false], |
| 44 | + ])(`%s (%s)`, (code, expected) => { |
| 45 | + const sourceFile = parse(code); |
| 46 | + const node = findDescendant(sourceFile, isDescendantOfXStateImport); |
| 47 | + |
| 48 | + if (expected) { |
| 49 | + expect(node).not.toBeUndefined(); |
| 50 | + } else { |
| 51 | + expect(node).toBeUndefined(); |
| 52 | + } |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('isXStateImportClause', () => { |
| 57 | + test.each<[code: string, expected: boolean]>([ |
| 58 | + [`import xstate from 'xstate'`, true], |
| 59 | + [`import { Machine } from 'xstate'`, false], |
| 60 | + [`import { Machine as M } from 'xstate'`, false], |
| 61 | + [`import xstate from 'not-xstate'`, false], |
| 62 | + [`import { Machine } from 'not-xstate'`, false], |
| 63 | + [`import { Machine as M } from 'not-xstate'`, false], |
| 64 | + ])('%s (%s)', (code, expected) => { |
| 65 | + const sourceFile = parse(code); |
| 66 | + const node = findDescendant(sourceFile, isXStateImportClause); |
| 67 | + |
| 68 | + if (expected) { |
| 69 | + expect(node).not.toBeUndefined(); |
| 70 | + expect(node!.kind).toBe(ts.SyntaxKind.ImportClause); |
| 71 | + } else { |
| 72 | + expect(node).toBeUndefined(); |
| 73 | + } |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('isMachineNamedImportSpecifier', () => { |
| 78 | + test.each<[code: string, expected: boolean]>([ |
| 79 | + [`import { Machine } from 'xstate'`, true], |
| 80 | + [`import { Machine as M } from 'xstate'`, true], |
| 81 | + [`import { NotMachine } from 'xstate'`, false], |
| 82 | + [`import { NotMachine as Machine } from 'xstate'`, false], |
| 83 | + [`console.log('not an ImportSpecifier')`, false], |
| 84 | + ])('%s (%s)', (code, expected) => { |
| 85 | + const sourceFile = parse(code); |
| 86 | + const node = findDescendant(sourceFile, isMachineNamedImportSpecifier); |
| 87 | + |
| 88 | + if (expected) { |
| 89 | + expect(node).not.toBeUndefined(); |
| 90 | + expect(node!.kind).toBe(ts.SyntaxKind.ImportSpecifier); |
| 91 | + } else { |
| 92 | + expect(node).toBeUndefined(); |
| 93 | + } |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('isMachineCallExpression', () => { |
| 98 | + test.each<[code: string, expected: boolean]>([ |
| 99 | + [ |
| 100 | + outdent` |
| 101 | + import { Machine } from 'xstate' |
| 102 | + const machine = Machine({}) |
| 103 | + `, |
| 104 | + true, |
| 105 | + ], |
| 106 | + [ |
| 107 | + outdent` |
| 108 | + import { Machine as M } from 'xstate' |
| 109 | + const machine = M({}) |
| 110 | + `, |
| 111 | + false, |
| 112 | + ], |
| 113 | + [ |
| 114 | + outdent` |
| 115 | + import xstate from 'xstate' |
| 116 | + const machine = xstate.Machine({}) |
| 117 | + `, |
| 118 | + false, |
| 119 | + ], |
| 120 | + [ |
| 121 | + outdent` |
| 122 | + import { Machine } from 'not-xstate' |
| 123 | + const machine = Machine({}) |
| 124 | + `, |
| 125 | + false, |
| 126 | + ], |
| 127 | + [ |
| 128 | + outdent` |
| 129 | + function Machine() {} |
| 130 | + const machine = Machine({}) |
| 131 | + `, |
| 132 | + false, |
| 133 | + ], |
| 134 | + [ |
| 135 | + outdent` |
| 136 | + console.log('non-xstate CallExpression') |
| 137 | + `, |
| 138 | + false, |
| 139 | + ], |
| 140 | + [ |
| 141 | + outdent` |
| 142 | + const str = "not a CallExpression" |
| 143 | + `, |
| 144 | + false, |
| 145 | + ], |
| 146 | + ])('%s (%s)', (code, expected) => { |
| 147 | + const sourceFile = parse(code); |
| 148 | + const node = findDescendant(sourceFile, isMachineCallExpression); |
| 149 | + |
| 150 | + if (expected) { |
| 151 | + expect(node).not.toBeUndefined(); |
| 152 | + expect(node!.kind).toBe(ts.SyntaxKind.CallExpression); |
| 153 | + } else { |
| 154 | + expect(node).toBeUndefined(); |
| 155 | + } |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe('isMachinePropertyAccessExpression', () => { |
| 160 | + test.each<[input: string, expected: boolean]>([ |
| 161 | + [ |
| 162 | + outdent` |
| 163 | + import xstate from 'xstate' |
| 164 | + const machine = xstate.Machine({}) |
| 165 | + `, |
| 166 | + true, |
| 167 | + ], |
| 168 | + [ |
| 169 | + outdent` |
| 170 | + import xstate from 'xstate' |
| 171 | + const Machine = xstate.Machine |
| 172 | + `, |
| 173 | + true, |
| 174 | + ], |
| 175 | + [ |
| 176 | + outdent` |
| 177 | + import { Machine } from 'xstate' |
| 178 | + const machine = Machine({}) |
| 179 | + `, |
| 180 | + false, |
| 181 | + ], |
| 182 | + [ |
| 183 | + outdent` |
| 184 | + import { Machine as M } from 'xstate' |
| 185 | + const machine = M({}) |
| 186 | + `, |
| 187 | + false, |
| 188 | + ], |
| 189 | + [ |
| 190 | + outdent` |
| 191 | + import { Machine } from 'not-xstate' |
| 192 | + const machine = Machine({}) |
| 193 | + `, |
| 194 | + false, |
| 195 | + ], |
| 196 | + [ |
| 197 | + outdent` |
| 198 | + function Machine() {} |
| 199 | + const machine = Machine({}) |
| 200 | + `, |
| 201 | + false, |
| 202 | + ], |
| 203 | + [ |
| 204 | + outdent` |
| 205 | + console.log('non-xstate PropertyAccessExpression') |
| 206 | + `, |
| 207 | + false, |
| 208 | + ], |
| 209 | + [ |
| 210 | + outdent` |
| 211 | + const str = "not a CallExpression" |
| 212 | + `, |
| 213 | + false, |
| 214 | + ], |
| 215 | + ])('%s (%s)', (code, expected) => { |
| 216 | + const sourceFile = parse(code); |
| 217 | + const node = findDescendant(sourceFile, isMachinePropertyAccessExpression); |
| 218 | + |
| 219 | + if (expected) { |
| 220 | + expect(node).not.toBeUndefined(); |
| 221 | + expect(node!.kind).toBe(ts.SyntaxKind.PropertyAccessExpression); |
| 222 | + } else { |
| 223 | + expect(node).toBeUndefined(); |
| 224 | + } |
| 225 | + }); |
| 226 | +}); |
0 commit comments