-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby,gatsby-plugin-utils): feature toggle and availability (#3…
- Loading branch information
Showing
9 changed files
with
89 additions
and
8 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
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,28 @@ | ||
import { hasFeature } from "../has-feature" | ||
import apis from "gatsby/apis.json" | ||
|
||
jest.mock(`gatsby/apis.json`, () => { | ||
return {} | ||
}) | ||
|
||
describe(`hasFeature`, () => { | ||
it(`should return true if gatsby has the feature`, () => { | ||
apis.features = [`image-service`] | ||
expect(hasFeature(`image-service`)).toBe(true) | ||
}) | ||
it(`should return false if the feature has a typo`, () => { | ||
apis.features = [`image-service`] | ||
expect(hasFeature(`image-services`)).toBe(false) | ||
}) | ||
|
||
it(`should return false if gatsby doesn't have the feature`, () => { | ||
apis.features = [] | ||
expect(hasFeature(`image-service`)).toBe(false) | ||
}) | ||
|
||
it(`should return false if gatsby doesn't support features section yet`, () => { | ||
// @ts-ignore - we want to test old versions too | ||
delete apis.features | ||
expect(hasFeature(`image-service`)).toBe(false) | ||
}) | ||
}) |
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,16 @@ | ||
import type { AvailableFeatures } from "gatsby" | ||
|
||
/** | ||
* Check the readme for a list of available features. | ||
*/ | ||
export function hasFeature(name: AvailableFeatures): boolean { | ||
try { | ||
const availableAPIs = require(`gatsby/apis.json`) | ||
|
||
return !!availableAPIs?.features?.includes(name) | ||
} catch (e) { | ||
throw new Error( | ||
`Couldn't check available APIs. Make sure you are on gatsby version >=2.13.41` | ||
) | ||
} | ||
} |
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