Skip to content

Commit 86f1884

Browse files
hawkgsKeen Yee Liau
authored and
Keen Yee Liau
committed
test(@schematics/angular): insert after last occurrence util
1 parent 52e9819 commit 86f1884

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

packages/schematics/angular/utility/ast-utils_spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
addExportToModule,
1717
addProviderToModule,
1818
addSymbolToNgModuleMetadata,
19+
insertAfterLastOccurrence,
20+
findNodes,
1921
} from './ast-utils';
2022

2123

@@ -205,4 +207,45 @@ describe('ast utils', () => {
205207
expect(output).toMatch(/import { LogService } from '.\/log.service';/);
206208
expect(output).toMatch(/\},\r?\n\s*LogService\r?\n\s*\]/);
207209
});
210+
211+
describe('insertAfterLastOccurrence', () => {
212+
const filePath: string = './src/foo.ts';
213+
214+
it('should work for the default scenario', () => {
215+
const fileContent = `const arr = ['foo'];`;
216+
const source = getTsSource(filePath, fileContent);
217+
const arrayNode = findNodes(source.getChildren().shift() as ts.Node, ts.SyntaxKind.ArrayLiteralExpression);
218+
const elements = (arrayNode.pop() as ts.ArrayLiteralExpression).elements;
219+
220+
const change = insertAfterLastOccurrence(
221+
elements as any as ts.Node[],
222+
`, 'bar'`,
223+
filePath,
224+
elements.pos,
225+
ts.SyntaxKind.StringLiteral
226+
);
227+
const output = applyChanges(filePath, fileContent, [change]);
228+
229+
expect(output).toMatch(/const arr = \['foo', 'bar'\];/);
230+
});
231+
232+
233+
it('should work without occurrences', () => {
234+
const fileContent = `const arr = [];`;
235+
const source = getTsSource(filePath, fileContent);
236+
const arrayNode = findNodes(source.getChildren().shift() as ts.Node, ts.SyntaxKind.ArrayLiteralExpression);
237+
const elements = (arrayNode.pop() as ts.ArrayLiteralExpression).elements;
238+
239+
const change = insertAfterLastOccurrence(
240+
elements as any as ts.Node[],
241+
`'bar'`,
242+
filePath,
243+
elements.pos,
244+
ts.SyntaxKind.StringLiteral
245+
);
246+
const output = applyChanges(filePath, fileContent, [change]);
247+
248+
expect(output).toMatch(/const arr = \['bar'\];/);
249+
});
250+
});
208251
});

0 commit comments

Comments
 (0)