-
Notifications
You must be signed in to change notification settings - Fork 30
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
1 parent
c345646
commit 5854401
Showing
9 changed files
with
169 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
import { apiClient } from './api' | ||
import StoryblokClient from 'storyblok-js-client'; | ||
|
||
// Mock the StoryblokClient to prevent actual HTTP requests | ||
vi.mock('storyblok-js-client', () => { | ||
const StoryblokClientMock = vi.fn().mockImplementation((config) => { | ||
return { | ||
config, | ||
}; | ||
}); | ||
|
||
return { | ||
default: StoryblokClientMock, | ||
__esModule: true, // Important for ESM modules | ||
}; | ||
}); | ||
|
||
describe('Storyblok API Client', () => { | ||
beforeEach(() => { | ||
// Reset the module state before each test to ensure test isolation | ||
vi.resetModules(); | ||
}); | ||
|
||
it('should have a default region of "eu"', () => { | ||
const { region } = apiClient() | ||
expect(region).toBe('eu') | ||
}) | ||
|
||
it('should return the same client instance when called multiple times without changes', () => { | ||
const api1 = apiClient(); | ||
const client1 = api1.client; | ||
|
||
const api2 = apiClient(); | ||
const client2 = api2.client; | ||
|
||
expect(client1).toBe(client2); | ||
}); | ||
|
||
it('should set the region on the client', () => { | ||
const { setRegion } = apiClient(); | ||
setRegion('us'); | ||
const { region } = apiClient(); | ||
expect(region).toBe('us'); | ||
}) | ||
|
||
it('should set the access token on the client', () => { | ||
const { setAccessToken } = apiClient(); | ||
setAccessToken('test-token'); | ||
const { client } = apiClient(); | ||
expect(client.config.accessToken).toBe('test-token'); | ||
}) | ||
}) |
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,46 @@ | ||
import StoryblokClient from "storyblok-js-client"; | ||
import { regions } from "./constants"; | ||
|
||
export interface ApiClientState { | ||
region: string, | ||
accessToken: string, | ||
client: StoryblokClient | null | ||
} | ||
|
||
const state: ApiClientState = { | ||
region: 'eu', | ||
accessToken: '', | ||
client: null | ||
} | ||
|
||
export function apiClient() { | ||
if (!state.client) { | ||
createClient() | ||
} | ||
|
||
function createClient() { | ||
state.client = new StoryblokClient({ | ||
accessToken: state.accessToken, | ||
region: state.region | ||
}) | ||
} | ||
|
||
function setAccessToken(accessToken: string) { | ||
state.accessToken = accessToken | ||
state.client = null | ||
createClient() | ||
} | ||
|
||
function setRegion(region: string) { | ||
state.region = region | ||
state.client = null | ||
createClient() | ||
} | ||
|
||
return { | ||
region: state.region, | ||
client: state.client, | ||
setAccessToken, | ||
setRegion, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
export const login = () => { | ||
|
||
|
||
export const loginWithToken = () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Login') | ||
} | ||
|
||
export const loginWithEmailAndPassword = () => { | ||
|
||
} | ||
|
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 |
---|---|---|
@@ -1,3 +1,14 @@ | ||
export const commands = { | ||
LOGIN: 'login', | ||
} | ||
|
||
export const regions = { | ||
EU: 'eu', | ||
US: 'us', | ||
CN: 'cn' | ||
} | ||
|
||
export const DEFAULT_AGENT = { | ||
SB_Agent: 'SB-CLI', | ||
SB_Agent_Version: process.env.npm_package_version || '4.x' | ||
} |
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