-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor
ng-add
to make it easier to add moar tests (#974)
- Loading branch information
Showing
8 changed files
with
113 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
projects/ngx-meta/schematics/ng-add/testing/get-app-config-or-app-module-content.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Tree } from '@angular-devkit/schematics' | ||
import { getFileContent } from '../../testing/get-file-content' | ||
|
||
export const getAppConfigOrAppModuleContent = ( | ||
tree: Tree, | ||
standalone: boolean, | ||
) => | ||
standalone | ||
? getFileContent(tree, '/src/app/app.config.ts') | ||
: getFileContent(tree, '/src/app/app.module.ts') |
18 changes: 18 additions & 0 deletions
18
projects/ngx-meta/schematics/ng-add/testing/provider-test-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export class ProviderTestCase { | ||
readonly name: string | ||
readonly symbol: string | ||
readonly code: string | ||
readonly entrypoint: string | ||
|
||
constructor(opts: { | ||
name: string | ||
symbol: string | ||
code?: string | ||
entrypoint?: string | ||
}) { | ||
this.name = opts.name | ||
this.symbol = opts.symbol | ||
this.code = opts.code ?? `${this.symbol}()` | ||
this.entrypoint = opts.entrypoint ?? this.name | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
projects/ngx-meta/schematics/ng-add/testing/should-add-root-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ProviderTestCase } from './provider-test-case' | ||
import { Tree } from '@angular-devkit/schematics' | ||
import { expect, it } from '@jest/globals' | ||
import { getAppConfigOrAppModuleContent } from './get-app-config-or-app-module-content' | ||
import { LIB_NAME } from '../../testing/lib-name' | ||
import { stripWhitespace } from '../../testing/strip-whitespace' | ||
import { regexpEscape } from '../../testing/regexp-escape' | ||
|
||
export const shouldAddRootProvider = ( | ||
providerTestCase: ProviderTestCase, | ||
treeFactory: () => Tree, | ||
standalone: boolean, | ||
) => { | ||
it(`should add ${providerTestCase.name} provider`, () => { | ||
const appConfigOrAppModuleContents = getAppConfigOrAppModuleContent( | ||
treeFactory(), | ||
standalone, | ||
) | ||
expect(appConfigOrAppModuleContents).toContain( | ||
`import { ${providerTestCase.symbol} } from '${LIB_NAME}/${providerTestCase.entrypoint}`, | ||
) | ||
expect(stripWhitespace(appConfigOrAppModuleContents)).toMatch( | ||
new RegExp(`providers:\\[.*${regexpEscape(providerTestCase.code)}.*]`), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing' | ||
import { Schema as NgNewSchema } from '@schematics/angular/ng-new/schema' | ||
|
||
// https://github.com/FortAwesome/angular-fontawesome/blob/0.15.0/projects/schematics/src/ng-add/index.spec.ts#L107 | ||
// https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/testing/test-app.ts | ||
export const createTestApp = async ( | ||
runner: SchematicTestRunner, | ||
options: Omit<NgNewSchema, 'version'> & Partial<Pick<NgNewSchema, 'version'>>, | ||
) => { | ||
return runner.runExternalSchematic<NgNewSchema>( | ||
'@schematics/angular', | ||
'ng-new', | ||
{ | ||
version: '9.0.0', | ||
directory: '.', | ||
...options, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Tree } from '@angular-devkit/schematics' | ||
|
||
// https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/testing/file-content.ts | ||
export const getFileContent = (tree: Tree, filePath: string): string => { | ||
const contentBuffer = tree.read(filePath) | ||
|
||
if (!contentBuffer) { | ||
throw new Error(`Cannot read "${filePath}" because it does not exist.`) | ||
} | ||
|
||
return contentBuffer.toString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// https://stackoverflow.com/a/9310752/3263250 | ||
// https://github.com/tc39/proposal-regex-escaping | ||
export const regexpEscape = (string: string) => | ||
string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// https://github.com/angular/angular-cli/blob/18.2.9/packages/schematics/angular/utility/standalone/rules_spec.ts#L45-L47 | ||
export const stripWhitespace = (value: string) => value.replace(/\s/g, '') |