-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix js-experimental ImportMap by not rendering empty statements
- Loading branch information
1 parent
9388a84
commit f0ef13f
Showing
3 changed files
with
91 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@metaplex-foundation/kinobi': patch | ||
--- | ||
|
||
Fix js-experimental ImportMap by not rendering empty statements |
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
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,77 @@ | ||
import test from 'ava'; | ||
import { ImportMap } from '../../../src/renderers/js-experimental/ImportMap'; | ||
|
||
test('it renders JavaScript import statements', (t) => { | ||
// Given an import map with 3 imports from 2 sources. | ||
const importMap = new ImportMap() | ||
.add('@solana/addresses', ['getAddressEncoder', 'Address']) | ||
.add('@solana/transactions', 'Transaction'); | ||
|
||
// When we render it. | ||
const importStatements = importMap.toString(); | ||
|
||
// Then we expect the following import statements. | ||
t.is( | ||
importStatements, | ||
"import { Address, getAddressEncoder } from '@solana/addresses';\n" + | ||
"import { Transaction } from '@solana/transactions';" | ||
); | ||
}); | ||
|
||
test('it renders JavaScript import aliases', (t) => { | ||
// Given an import map with an import alias. | ||
const importMap = new ImportMap() | ||
.add('@solana/addresses', 'Address') | ||
.addAlias('@solana/addresses', 'Address', 'SolanaAddress'); | ||
|
||
// When we render it. | ||
const importStatements = importMap.toString(); | ||
|
||
// Then we expect the following import statement. | ||
t.is( | ||
importStatements, | ||
"import { Address as SolanaAddress } from '@solana/addresses';" | ||
); | ||
}); | ||
|
||
test('it offers some default dependency mappings', (t) => { | ||
// Given an import map with some recognized dependency keys. | ||
const importMap = new ImportMap() | ||
.add('solanaAddresses', 'Address') | ||
.add('solanaCodecsCore', 'Codec') | ||
.add('generatedTypes', 'MyType') | ||
.add('shared', 'myHelper') | ||
.add('hooked', 'MyCustomType'); | ||
|
||
// When we render it. | ||
const importStatements = importMap.toString(); | ||
|
||
// Then we expect the following import statements. | ||
t.is( | ||
importStatements, | ||
"import { Address } from '@solana/addresses';\n" + | ||
"import { Codec } from '@solana/codecs-core';\n" + | ||
"import { MyCustomType } from '../../hooked';\n" + | ||
"import { myHelper } from '../shared';\n" + | ||
"import { MyType } from '../types';" | ||
); | ||
}); | ||
|
||
test('it supports custom dependency mappings', (t) => { | ||
// Given an import map with some custom dependency keys. | ||
const importMap = new ImportMap().add('myDependency', 'MyType'); | ||
|
||
// When we render it whilst providing custom dependency mappings. | ||
const importStatements = importMap.toString({ | ||
myDependency: 'my/custom/path', | ||
}); | ||
|
||
// Then we expect the following import statement. | ||
t.is(importStatements, "import { MyType } from 'my/custom/path';"); | ||
}); | ||
|
||
test('it does not render empty import statements', (t) => { | ||
t.is(new ImportMap().toString(), ''); | ||
t.is(new ImportMap().add('shared', []).toString(), ''); | ||
t.is(new ImportMap().addAlias('shared', 'Foo', 'Bar').toString(), ''); | ||
}); |