Skip to content

Commit

Permalink
Merge pull request #198 from skedify/develop
Browse files Browse the repository at this point in the history
release/next
  • Loading branch information
timothy-suy authored Sep 8, 2020
2 parents d7aabca + 6fb9e15 commit 7f120ae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
12 changes: 10 additions & 2 deletions src/build/Skedify.testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export {
/**
* Expose an install mock function
*/
export function installSkedifySDKMock(instance) {
install(get(instance).network)
export function installSkedifySDKMock(instance, options = {}) {
install(
get(instance).network,
Object.assign(
{
mockAccessTokensCall: true,
},
options
)
)
}

/**
Expand Down
60 changes: 35 additions & 25 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,37 @@ export function mostRecentRequest() {
return requests[requests.length - 1]
}

const SETUP_REQUESTS = [
{
url: /access_tokens/,
response: {
status: 200,
data: {
access_token: 'fake_example_access_token',
token_type: 'Bearer',
expires_in: 5400,
},
const ACCESS_TOKEN_CALL = {
url: /access_tokens/,
response: {
status: 200,
data: {
access_token: 'fake_example_access_token',
token_type: 'Bearer',
expires_in: 5400,
},
},
{
url: /\/integrations\/proxy/,
response: {
status: 200,
}

const PROXY_CALL = {
url: /\/integrations\/proxy/,
response: {
status: 200,
data: {
data: {
data: {
// We need to use a getter to delay the execution of this function
// We need access to the mostRecent request, but if there is no most recent
// Request we can't access its url. Therefor once we are sure the /integrations/proxy
// Is executed, we can safely run the mostRecent() function.
get url() {
return mostRecentRequest().url.replace('/integrations/proxy', '')
},
// We need to use a getter to delay the execution of this function
// We need access to the mostRecent request, but if there is no most recent
// Request we can't access its url. Therefor once we are sure the /integrations/proxy
// Is executed, we can safely run the mostRecent() function.
get url() {
return mostRecentRequest().url.replace('/integrations/proxy', '')
},
},
},
},
]
}

const SETUP_REQUESTS = [{ ...ACCESS_TOKEN_CALL }, { ...PROXY_CALL }]

export function mockedRequests() {
const { requests, ignoredRequests } = storage()
Expand All @@ -124,7 +125,14 @@ export function mockedRequests() {
.filter((request) => !ignoredRequests.includes(request))
}

export function install(instance) {
export function install(instance, options = {}) {
const { mockAccessTokensCall } = Object.assign(
{
mockAccessTokensCall: true,
},
options
)

defaultAdapters.set(instance, instance.defaults.adapter)
instance.defaults.adapter = mockAdapter

Expand All @@ -135,8 +143,10 @@ export function install(instance) {
ignoredRequests: [],
})

const setup_calls = mockAccessTokensCall ? SETUP_REQUESTS : [PROXY_CALL]

// Stub setup requests
SETUP_REQUESTS.forEach(({ url, response }) => {
setup_calls.forEach(({ url, response }) => {
mockMatchingURLResponse(
url,
response.data,
Expand Down

0 comments on commit 7f120ae

Please sign in to comment.