forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks_spec.ts
65 lines (54 loc) · 2.11 KB
/
hooks_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @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.dev/license
*/
import { Hooks } from '../src/hooks';
describe('Hooks', () => {
let hooks: Hooks & { run: Function };
const url = new URL('http://example.com/');
beforeEach(() => {
hooks = new Hooks() as Hooks & { run: Function };
});
describe('on', () => {
it('should register a pre-transform hook', () => {
hooks.on('html:transform:pre', (ctx) => '');
expect(hooks.has('html:transform:pre')).toBeTrue();
});
it('should register multiple hooks under the same name', () => {
hooks.on('html:transform:pre', (ctx) => '');
hooks.on('html:transform:pre', (ctx) => '');
expect(hooks.has('html:transform:pre')).toBeTrue();
});
});
describe('run', () => {
it('should execute pre-transform hooks in sequence', async () => {
hooks.on('html:transform:pre', ({ html }) => html + '1');
hooks.on('html:transform:pre', ({ html }) => html + '2');
const result = await hooks.run('html:transform:pre', { html: 'start', url });
expect(result).toBe('start12');
});
it('should return the context html if no hooks are registered', async () => {
const result = await hooks.run('html:transform:pre', { html: 'start', url });
expect(result).toBe('start');
});
it('should throw an error for unknown hook names', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await expectAsync(hooks.run('unknown:hook' as any, { html: 'start' })).toBeRejectedWithError(
'Running hook "unknown:hook" is not supported.',
);
});
});
describe('has', () => {
it('should return true if a hook is registered', () => {
const handler = (ctx: { html: string }) => '';
hooks.on('html:transform:pre', handler);
expect(hooks.has('html:transform:pre')).toBeTrue();
});
it('should return false if no hook is registered', () => {
expect(hooks.has('html:transform:pre')).toBeFalse();
});
});
});