|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +/*global jest, describe, beforeEach, it, expect*/ |
| 12 | + |
| 13 | +jest.autoMockOff(); |
| 14 | + |
| 15 | +describe('getParameterName', () => { |
| 16 | + let getParameterName; |
| 17 | + let expression; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + getParameterName = require('../getParameterName'); |
| 21 | + ({expression} = require('../../../tests/utils')); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns the name for a normal parameter', () => { |
| 25 | + const def = expression('function(a) {}'); |
| 26 | + const param = def.get('params', 0); |
| 27 | + expect(getParameterName(param)).toEqual('a'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns the name for a rest parameter', () => { |
| 31 | + const def = expression('function(...a) {}'); |
| 32 | + const param = def.get('params', 0); |
| 33 | + expect(getParameterName(param)).toEqual('...a'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns the name for a parameter with a default value', () => { |
| 37 | + const def = expression('function(a = 0) {}'); |
| 38 | + const param = def.get('params', 0); |
| 39 | + expect(getParameterName(param)).toEqual('a'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns the raw object representation for a parameter with destructuring', () => { |
| 43 | + const def = expression('function({a}) {}'); |
| 44 | + const param = def.get('params', 0); |
| 45 | + expect(getParameterName(param)).toEqual('{a}'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('throws when passed an invalid path', () => { |
| 49 | + const def = expression('function() {}'); |
| 50 | + const param = def; |
| 51 | + expect(() => getParameterName(param)).toThrow(); |
| 52 | + }); |
| 53 | +}); |
0 commit comments