-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
153 additions
and
38 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
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,9 +1,60 @@ | ||
const httpClientMock = { | ||
get: jest.fn().mockResolvedValue({ data: {} }), | ||
post: jest.fn().mockResolvedValue({ data: {} }), | ||
put: jest.fn().mockResolvedValue({ data: {} }), | ||
delete: jest.fn().mockResolvedValue({ data: {} }), | ||
// ... other methods that your httpClient might have | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { Mock } from 'jest-mock'; | ||
|
||
interface MockResponse { | ||
[key: string]: any; | ||
} | ||
|
||
interface HttpClientMock { | ||
get: Mock<any, [string, AxiosRequestConfig?]>; | ||
post: Mock<any, [string, any, AxiosRequestConfig?]>; | ||
put: Mock<any, [string, any, AxiosRequestConfig?]>; | ||
delete: Mock<any, [string, AxiosRequestConfig?]>; | ||
__setMockResponse: (method: HttpMethod, url: string, params: Record<string, any>, response: any) => void; | ||
__resetMockResponses: () => void; | ||
} | ||
|
||
type HttpMethod = 'get' | 'post' | 'put' | 'delete'; | ||
|
||
const mockResponses: Record<string, MockResponse> = {}; | ||
|
||
const httpClientMock: HttpClientMock = { | ||
get: jest.fn((url: string, config?: AxiosRequestConfig) => { | ||
return resolveResponse('get', url, config); | ||
}), | ||
post: jest.fn((url: string, data: any, config?: AxiosRequestConfig) => { | ||
return resolveResponse('post', url, config, data); | ||
}), | ||
put: jest.fn((url: string, data: any, config?: AxiosRequestConfig) => { | ||
return resolveResponse('put', url, config, data); | ||
}), | ||
delete: jest.fn((url: string, config?: AxiosRequestConfig) => { | ||
return resolveResponse('delete', url, config); | ||
}), | ||
__setMockResponse: (method: HttpMethod, url: string, params: Record<string, any>, response: any) => { | ||
const key = `${method}:${url}:${JSON.stringify(params) || ''}`; | ||
mockResponses[key] = response; | ||
}, | ||
__resetMockResponses: () => { | ||
Object.keys(mockResponses).forEach(key => delete mockResponses[key]); | ||
}, | ||
}; | ||
|
||
const standardErrorResponses = { | ||
unauthorized: { status: 401, data: { message: 'Not authorized' } }, | ||
serverError: { status: 500, data: { message: 'Internal server error' } }, | ||
badRequest: { status: 400, data: { message: 'Bad request' } }, | ||
}; | ||
|
||
export default httpClientMock; | ||
function resolveResponse(method: HttpMethod, url: string, config?: AxiosRequestConfig, data?: any) { | ||
const key = `${method}:${url}:${JSON.stringify(config?.params) || ''}`; | ||
if (mockResponses[key]) { | ||
return Promise.resolve({ data: mockResponses[key] }); | ||
} else if (mockResponses[method] && mockResponses[method][url]) { | ||
return Promise.resolve({ data: mockResponses[method][url] }); | ||
} else { | ||
return Promise.resolve({ data: [] }); // Default response is an empty array | ||
} | ||
} | ||
|
||
export { httpClientMock, standardErrorResponses }; |
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,57 @@ | ||
import { RxCollection, RxPlugin } from 'rxdb'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
/** | ||
* Generate a UUID if the primary key is not set | ||
*/ | ||
export function generateID(this: RxCollection, data: Record<string, any>) { | ||
const primaryPath = this.schema.primaryPath; | ||
const hasMetaData = this.schema.jsonSchema.properties.meta_data; | ||
let metaUUID; | ||
|
||
if (hasMetaData) { | ||
data.meta_data = data.meta_data || []; | ||
const meta = data.meta_data.find((meta: any) => meta.key === '_woocommerce_pos_uuid'); | ||
metaUUID = meta && meta.value; | ||
} | ||
|
||
if (!data[primaryPath] && metaUUID) { | ||
data[primaryPath] = metaUUID; | ||
} else if (!data[primaryPath]) { | ||
const uuid = uuidv4(); | ||
// | ||
if (primaryPath === 'uuid') { | ||
data.uuid = uuid; | ||
} else if (primaryPath === 'logId') { | ||
data.logId = uuid; | ||
} else if (primaryPath === 'localID') { | ||
data.localID = uuid.slice(0, 8); // only short id required here | ||
} | ||
} | ||
|
||
if (hasMetaData && !metaUUID) { | ||
data.meta_data.push({ | ||
key: '_woocommerce_pos_uuid', | ||
value: data[primaryPath], | ||
}); | ||
} | ||
} | ||
|
||
export const RxDBGenerateIdPlugin: RxPlugin = { | ||
name: 'generate-id', | ||
rxdb: true, | ||
prototypes: { | ||
RxCollection: (proto: any) => { | ||
proto.generateID = generateID; | ||
}, | ||
}, | ||
overwritable: {}, | ||
hooks: { | ||
createRxCollection: { | ||
after({ collection }) { | ||
collection.preInsert(generateID, false); | ||
collection.preSave(generateID, 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
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