Skip to content

Commit

Permalink
mockViewMany: Add support for partial Packuments.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 23, 2023
1 parent 6d01108 commit 85c2bc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,30 @@ export async function packageAuthorChanged(
return false
}

/** Returns true if an object is a Packument. */
const isPackument = (o: any): o is Partial<Packument> => o && (o.name || o.engines || o.version || o.versions)

/** Creates a function with the same signature as viewMany that always returns the given versions. */
export const mockViewMany =
(mockReturnedVersions: MockedVersions) =>
(name: string, fields: string[], currentVersion: Version, options: Options): Promise<Packument> => {
const version =
// a partial Packument
const partialPackument =
typeof mockReturnedVersions === 'function'
? mockReturnedVersions(options)?.[name]
: typeof mockReturnedVersions === 'string'
: typeof mockReturnedVersions === 'string' || isPackument(mockReturnedVersions)
? mockReturnedVersions
: mockReturnedVersions[name]

const version = isPackument(partialPackument) ? partialPackument.version : partialPackument
const packument = {
name,
engines: { node: '' },
time: { [version || '']: new Date().toISOString() },
version: version || '',
// versions are not needed in nested packument
versions: [],
...(isPackument(partialPackument) ? partialPackument : null),
}

return Promise.resolve({
Expand Down
8 changes: 7 additions & 1 deletion src/types/MockedVersions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Index } from './IndexType'
import { Options } from './Options'
import { Packument } from './Packument'
import { Version } from './Version'

export type MockedVersions = Version | Index<Version> | ((options: Options) => Index<Version> | null)
export type MockedVersions =
| Version
| Partial<Packument>
| Index<Version>
| Index<Partial<Packument>>
| ((options: Options) => Index<Version> | Index<Partial<Packument>> | null)

0 comments on commit 85c2bc8

Please sign in to comment.