angular-cn/aio/tools/transforms/examples-package/processors/check-for-unused-example-regions.spec.js
Pete Bacon Darwin fb97e67fcb build(docs-infra): fail if there are unused example regions (#40479)
We can define regions in our examples that can be referenced
and rendered in guides as code snippets. It is quite hard to ensure
that these regions are maintained correctly. One reason for this is
it is hard to know whether a region is being used or not.

This commit adds a new processor that checks for unused named
regions in examples and fails if any are found.

Fixes #19761

PR Close #40479
2021-01-20 16:12:15 -08:00

78 lines
2.3 KiB
JavaScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const testPackage = require('../../helpers/test-package');
const Dgeni = require('dgeni');
describe('checkForUnusedExampleRegions', () => {
var processor, exampleMap;
beforeEach(function() {
const dgeni = new Dgeni([testPackage('examples-package', true)]);
const injector = dgeni.configureInjector();
exampleMap = injector.get('exampleMap');
processor = injector.get('checkForUnusedExampleRegions');
});
describe('$process()', () => {
it('should error if there is a named example region which has not been used', () => {
exampleMap['examples'] = {
'some/file': {
regions: {
'': {id: 'some/file#'},
'used': {id: 'some/file#used', usedInDoc: {}},
'not-used': {id: 'some/file#not-used'},
}
}
};
expect(() => processor.$process())
.toThrowError('There is 1 unused example region:\n - some/file#not-used');
});
it('should not error if there are no example folders', () => {
expect(() => processor.$process()).not.toThrowError();
});
it('should not error if there are no example files', () => {
exampleMap['examples'] = {};
expect(() => processor.$process()).not.toThrowError();
});
it('should not error if there are no example regions', () => {
exampleMap['examples'] = {
'some/file': {
regions: {},
},
};
expect(() => processor.$process()).not.toThrowError();
});
it('should not error if there are no named example regions', () => {
exampleMap['examples'] = {
'some/file': {
regions: {
'': {id: 'some/file#'},
},
},
};
expect(() => processor.$process()).not.toThrowError();
});
it('should not error if there are no unused named example regions', () => {
exampleMap['examples'] = {
'some/file': {
regions: {
'': {id: 'some/file#'},
'used': {id: 'some/file#used', usedInDoc: {}},
}
}
};
expect(() => processor.$process()).not.toThrowError();
});
});
});