-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,117 additions
and
907 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,93 @@ | ||
import type { Config, RuntimeContext, TransformContext } from '../types.js'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import fs from 'fs-extra'; | ||
import { | ||
FIELD_TYPE_LINK, | ||
LINK_TYPE_ENTRY, | ||
LINK_TYPE_ASSET, | ||
getFieldSettings, | ||
} from '../helper/contentful.js'; | ||
import { HookManager } from '../helper/hook-manager.js'; | ||
|
||
const cache = new Map(); | ||
|
||
export const readFixture = async (file) => { | ||
if (!cache.has(file)) { | ||
const content = await fs.readJSON(path.join(__dirname, 'fixtures', file)); | ||
cache.set(file, content); | ||
} | ||
|
||
return cache.get(file); | ||
}; | ||
|
||
export const readFixtureSync = (file) => { | ||
if (!cache.has(file)) { | ||
const content = fs.readJSONSync(path.join(__dirname, 'fixtures', file)); | ||
cache.set(file, content); | ||
} | ||
|
||
return cache.get(file); | ||
}; | ||
|
||
export const getContent = async () => { | ||
const assets = await readFixture('assets.json'); | ||
const entries = await readFixture('entries.json'); | ||
const locales = await readFixture('locales.json'); | ||
const contentTypes = await readFixture('content_types.json'); | ||
|
||
const [entry] = entries; | ||
const [asset] = assets; | ||
const assetLink = { | ||
sys: { | ||
id: 'asset-id', | ||
type: FIELD_TYPE_LINK, | ||
linkType: LINK_TYPE_ASSET, | ||
}, | ||
}; | ||
|
||
const entryLink = { | ||
sys: { | ||
id: 'entry-id', | ||
type: FIELD_TYPE_LINK, | ||
linkType: LINK_TYPE_ENTRY, | ||
}, | ||
}; | ||
|
||
return { entries, assets, contentTypes, locales, assetLink, entryLink, entry, asset }; | ||
}; | ||
|
||
export const getConfig = (fixture: Partial<Config> = {}): Config => { | ||
return { | ||
...fixture, | ||
} as Config; | ||
}; | ||
export const getRuntimeContext = (fixture: Partial<RuntimeContext> = {}): RuntimeContext => { | ||
const assets = readFixtureSync('assets.json'); | ||
const entries = readFixtureSync('entries.json'); | ||
const locales = readFixtureSync('locales.json'); | ||
const contentTypes = readFixtureSync('content_types.json'); | ||
|
||
const fieldSettings = getFieldSettings(contentTypes); | ||
const { code: defauleLocale } = locales.find((locale) => locale.default) || locales[0]; | ||
|
||
const result = { | ||
config: getConfig(), | ||
localized: new Map(), | ||
data: { | ||
assets, | ||
entries, | ||
contentTypes, | ||
locales, | ||
fieldSettings, | ||
}, | ||
defauleLocale, | ||
...fixture, | ||
}; | ||
|
||
const hooks = new HookManager(result as RuntimeContext, getConfig()); | ||
|
||
return { ...result, hooks } as RuntimeContext; | ||
}; | ||
export const getTransformContext = (fixture: Partial<TransformContext> = {}): TransformContext => | ||
({ assets:[], entries:[], assetMap: new Map(), entryMap: new Map(), ...fixture } as TransformContext); |
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
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
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
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
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,30 @@ | ||
import { Entry } from '../types.js'; | ||
import { collect } from './utils.js'; | ||
|
||
test('collect', () => { | ||
const data = [ | ||
{ sys: { id: 1 }, fields: { slug: 'a' } }, | ||
{ sys: { id: 2 }, fields: { slug: 'b', parent: 1 } }, | ||
{ sys: { id: 3 }, fields: { slug: 'c', parent: 2 } }, | ||
{ sys: { id: 4 }, fields: { slug: 'd', parent: 3 } }, | ||
{ sys: { id: 5 }, fields: { slug: 'e', parent: 4 } }, | ||
]; | ||
|
||
const getter = { | ||
getId: (item) => item.sys.id, | ||
getNextId: (item) => item.fields.parent, | ||
getValue: (item) => item.fields.slug, | ||
}; | ||
|
||
const a = collect(data[4] as unknown as Entry, data as unknown as Entry[], { | ||
...getter, | ||
reverse: true, | ||
}); | ||
const b = collect(data[4] as unknown as Entry, data as unknown as Entry[], { | ||
...getter, | ||
reverse: false, | ||
}); | ||
|
||
expect(a).toEqual(['a', 'b', 'c', 'd', 'e']); | ||
expect(b).toEqual(['e', 'd', 'c', 'b', 'a']); | ||
}); |
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
Oops, something went wrong.