|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { _addTsDocTagToExports } from '../TsDocTagHelpers'; |
| 5 | + |
| 6 | +describe(_addTsDocTagToExports.name, () => { |
| 7 | + test('injects tag into an existing JSDoc comment before an export', () => { |
| 8 | + const input: string = ['/**', ' * A description.', ' */', 'export type Foo = {};'].join('\n'); |
| 9 | + |
| 10 | + expect(_addTsDocTagToExports(input, '@beta')).toMatchSnapshot(); |
| 11 | + }); |
| 12 | + |
| 13 | + test('creates a new JSDoc block for an export without a preceding comment', () => { |
| 14 | + const input: string = 'export type Foo = {};'; |
| 15 | + |
| 16 | + expect(_addTsDocTagToExports(input, '@public')).toMatchSnapshot(); |
| 17 | + }); |
| 18 | + |
| 19 | + test('handles multiple exports with and without JSDoc comments', () => { |
| 20 | + const input: string = [ |
| 21 | + '/**', |
| 22 | + ' * First type.', |
| 23 | + ' */', |
| 24 | + 'export type Foo = {};', |
| 25 | + '', |
| 26 | + 'export type Bar = {};' |
| 27 | + ].join('\n'); |
| 28 | + |
| 29 | + expect(_addTsDocTagToExports(input, '@beta')).toMatchSnapshot(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('normalizes CRLF line endings to LF', () => { |
| 33 | + const input: string = '/**\r\n * A description.\r\n */\r\nexport type Foo = {};'; |
| 34 | + |
| 35 | + expect(_addTsDocTagToExports(input, '@beta')).toMatchSnapshot(); |
| 36 | + }); |
| 37 | + |
| 38 | + test('does not double-tag an export that already has a JSDoc block', () => { |
| 39 | + const input: string = [ |
| 40 | + '/**', |
| 41 | + ' * Already documented.', |
| 42 | + ' */', |
| 43 | + 'export interface IConfig {', |
| 44 | + ' name: string;', |
| 45 | + '}' |
| 46 | + ].join('\n'); |
| 47 | + |
| 48 | + const result: string = _addTsDocTagToExports(input, '@public'); |
| 49 | + |
| 50 | + // The tag should appear exactly once |
| 51 | + const tagOccurrences: number = (result.match(/@public/g) || []).length; |
| 52 | + expect(tagOccurrences).toBe(1); |
| 53 | + expect(result).toMatchSnapshot(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('does not modify non-export lines', () => { |
| 57 | + const input: string = ['// A leading comment', 'const internal = 1;', '', 'export type Foo = {};'].join( |
| 58 | + '\n' |
| 59 | + ); |
| 60 | + |
| 61 | + expect(_addTsDocTagToExports(input, '@beta')).toMatchSnapshot(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments