|
14 | 14 | const { |
15 | 15 | extractNativeModuleName, |
16 | 16 | createParserErrorCapturer, |
| 17 | + visit, |
17 | 18 | } = require('../utils.js'); |
18 | 19 | const {ParserError} = require('../errors'); |
19 | 20 |
|
@@ -115,3 +116,107 @@ describe('createParserErrorCapturer', () => { |
115 | 116 | }); |
116 | 117 | }); |
117 | 118 | }); |
| 119 | + |
| 120 | +describe('visit', () => { |
| 121 | + describe('when the astNode is null', () => { |
| 122 | + it("doesn't call the visitor function", () => { |
| 123 | + const visitorFunction = jest.fn(); |
| 124 | + const visitor = { |
| 125 | + itemType: visitorFunction, |
| 126 | + }; |
| 127 | + |
| 128 | + const astNode = null; |
| 129 | + |
| 130 | + visit(astNode, visitor); |
| 131 | + |
| 132 | + expect(visitorFunction).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('when the astNode is not an object', () => { |
| 137 | + it("doesn't call the visitor function", () => { |
| 138 | + const visitorFunction = jest.fn(); |
| 139 | + const visitor = { |
| 140 | + itemType: visitorFunction, |
| 141 | + }; |
| 142 | + |
| 143 | + const astNode = 'astNode'; |
| 144 | + |
| 145 | + visit(astNode, visitor); |
| 146 | + |
| 147 | + expect(visitorFunction).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('when the astNode is an object', () => { |
| 152 | + describe("when the astNode has a string type that doesn't exist in the visitor object", () => { |
| 153 | + it("doesn't call the visitor function", () => { |
| 154 | + const visitorFunction = jest.fn(); |
| 155 | + const visitor = { |
| 156 | + itemType: visitorFunction, |
| 157 | + }; |
| 158 | + |
| 159 | + const astNode = {type: 'itemTypeNotInVisitor'}; |
| 160 | + |
| 161 | + visit(astNode, visitor); |
| 162 | + |
| 163 | + expect(visitorFunction).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('when the astNode has a string type that exists in the visitor object', () => { |
| 168 | + it("doesn't call the visitor function", () => { |
| 169 | + const visitorFunction = jest.fn(); |
| 170 | + const visitor = { |
| 171 | + itemType: visitorFunction, |
| 172 | + }; |
| 173 | + |
| 174 | + const astNode = {type: 'itemType'}; |
| 175 | + |
| 176 | + visit(astNode, visitor); |
| 177 | + |
| 178 | + expect(visitorFunction).toHaveBeenCalledTimes(1); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe("when the astNode doesn't have a string type", () => { |
| 183 | + it('iterates on every values of the astNode', () => { |
| 184 | + const visitorFunction = jest.fn(); |
| 185 | + const visitor = { |
| 186 | + itemType1: visitorFunction, |
| 187 | + itemType2: visitorFunction, |
| 188 | + }; |
| 189 | + |
| 190 | + const astNode = { |
| 191 | + firstChildNode: {type: 'itemType1'}, |
| 192 | + secondChildNode: {type: 'itemType2'}, |
| 193 | + thirdChildNode: {type: 'itemType3'}, |
| 194 | + }; |
| 195 | + |
| 196 | + visit(astNode, visitor); |
| 197 | + |
| 198 | + expect(visitorFunction).toHaveBeenCalledTimes(2); |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('when the astNode is an array', () => { |
| 204 | + it('iterates on every values of the astNode', () => { |
| 205 | + const visitorFunction = jest.fn(); |
| 206 | + const visitor = { |
| 207 | + itemType1: visitorFunction, |
| 208 | + itemType2: visitorFunction, |
| 209 | + }; |
| 210 | + |
| 211 | + const astNode = [ |
| 212 | + {type: 'itemType1'}, |
| 213 | + {type: 'itemType2'}, |
| 214 | + {type: 'itemType3'}, |
| 215 | + ]; |
| 216 | + |
| 217 | + visit(astNode, visitor); |
| 218 | + |
| 219 | + expect(visitorFunction).toHaveBeenCalledTimes(2); |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments