Revised @woocommerce/api Test Suite
This commit replaces the moxios package with the axios-mock-adapter, in order to fix all of the failing tests. This felt necessary because changing the TypeScript configuration would have the largest potential impact on this package, due to it being written natively in it. This package is a great candidate for testing all of the new configuration changes out on.
This commit is contained in:
parent
c91dda177d
commit
2a29754ab2
16
package.json
16
package.json
|
@ -19,26 +19,26 @@
|
|||
"devDependencies": {
|
||||
"@automattic/nx-composer": "^0.1.0",
|
||||
"@nrwl/cli": "latest",
|
||||
"@nrwl/linter": "^13.1.3",
|
||||
"@nrwl/linter": "^13.1.4",
|
||||
"@nrwl/tao": "latest",
|
||||
"@nrwl/web": "^13.1.3",
|
||||
"@nrwl/web": "^13.1.4",
|
||||
"@nrwl/workspace": "latest",
|
||||
"@types/node": "14.14.33",
|
||||
"@woocommerce/eslint-plugin": "^1.2.0",
|
||||
"@wordpress/prettier-config": "^1.0.5",
|
||||
"@woocommerce/eslint-plugin": "^1.3.0",
|
||||
"@wordpress/prettier-config": "^1.1.1",
|
||||
"chalk": "^4.1.2",
|
||||
"glob": "^7.2.0",
|
||||
"jest": "^27.0.6",
|
||||
"jest": "^27.3.1",
|
||||
"mkdirp": "^1.0.4",
|
||||
"node-stream-zip": "^1.13.6",
|
||||
"prettier": "npm:wp-prettier@2.2.1-beta-1",
|
||||
"node-stream-zip": "^1.15.0",
|
||||
"prettier": "npm:wp-prettier@^2.2.1-beta-1",
|
||||
"request": "^2.88.2",
|
||||
"typescript": "4.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "7.12.9",
|
||||
"@wordpress/babel-plugin-import-jsx-pragma": "^3.1.0",
|
||||
"@wordpress/babel-preset-default": "^6.3.3",
|
||||
"@wordpress/babel-preset-default": "^6.3.4",
|
||||
"lodash": "^4.17.21",
|
||||
"wp-textdomain": "1.0.1"
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -34,23 +34,21 @@
|
|||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "0.21.2",
|
||||
"axios": "^0.24.0",
|
||||
"create-hmac": "1.1.7",
|
||||
"oauth-1.0a": "2.2.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/create-hmac": "1.1.0",
|
||||
"@types/jest": "25.2.1",
|
||||
"@types/moxios": "^0.4.9",
|
||||
"@types/jest": "^27.0.2",
|
||||
"@types/node": "13.13.5",
|
||||
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
||||
"@typescript-eslint/parser": "^5.3.0",
|
||||
"eslint": "^8.1.0",
|
||||
"jest": "^25.1.0",
|
||||
"jest-mock-extended": "^1.0.10",
|
||||
"moxios": "0.4.0",
|
||||
"ts-jest": "25.5.0",
|
||||
"typescript": "3.9.7"
|
||||
"@typescript-eslint/eslint-plugin": "^5.3.1",
|
||||
"@typescript-eslint/parser": "^5.3.1",
|
||||
"axios-mock-adapter": "^1.20.0",
|
||||
"eslint": "^8.2.0",
|
||||
"jest": "^27.3.1",
|
||||
"ts-jest": "^27.0.7",
|
||||
"typescript": "^4.4.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
|
|
@ -101,7 +101,7 @@ describe( 'ModelRepository', () => {
|
|||
|
||||
const created = await repository.create( { parent: 'yes' }, { childName: 'test' } );
|
||||
expect( created ).toBe( model );
|
||||
expect( callback ).toHaveBeenCalledWith( { childName: 'test' } );
|
||||
expect( callback ).toHaveBeenCalledWith( { parent: 'yes' }, { childName: 'test' } );
|
||||
} );
|
||||
|
||||
it( 'should throw error on create without callback', () => {
|
||||
|
|
|
@ -1,52 +1,54 @@
|
|||
import { mocked } from 'ts-jest/utils'
|
||||
import { ModelTransformerTransformation } from '../model-transformer-transformation';
|
||||
import { ModelTransformer } from '../../model-transformer';
|
||||
import { mock, MockProxy } from 'jest-mock-extended';
|
||||
import { DummyModel } from '../../../__test_data__/dummy-model';
|
||||
|
||||
jest.mock( '../../model-transformer' );
|
||||
|
||||
describe( 'ModelTransformerTransformation', () => {
|
||||
let mockTransformer: MockProxy< ModelTransformer< any > > & ModelTransformer< any >;
|
||||
let propertyTransformer: ModelTransformer< any >;
|
||||
let transformation: ModelTransformerTransformation< any >;
|
||||
|
||||
beforeEach( () => {
|
||||
mockTransformer = mock< ModelTransformer< any > >();
|
||||
propertyTransformer = new ModelTransformer( [] );
|
||||
transformation = new ModelTransformerTransformation< DummyModel >(
|
||||
'test',
|
||||
DummyModel,
|
||||
mockTransformer,
|
||||
propertyTransformer,
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'should execute child transformer', () => {
|
||||
mockTransformer.toModel.mockReturnValue( { toModel: 'Test' } );
|
||||
mocked( propertyTransformer.toModel ).mockReturnValue( { toModel: 'Test' } );
|
||||
|
||||
let transformed = transformation.toModel( { test: 'Test' } );
|
||||
|
||||
expect( transformed ).toMatchObject( { test: { toModel: 'Test' } } );
|
||||
expect( mockTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test' );
|
||||
expect( propertyTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test' );
|
||||
|
||||
mockTransformer.fromModel.mockReturnValue( { fromModel: 'Test' } );
|
||||
mocked( propertyTransformer.fromModel ).mockReturnValue( { fromModel: 'Test' } );
|
||||
|
||||
transformed = transformation.fromModel( { test: 'Test' } );
|
||||
|
||||
expect( transformed ).toMatchObject( { test: { fromModel: 'Test' } } );
|
||||
expect( mockTransformer.fromModel ).toHaveBeenCalledWith( 'Test' );
|
||||
expect( propertyTransformer.fromModel ).toHaveBeenCalledWith( 'Test' );
|
||||
} );
|
||||
|
||||
it( 'should execute child transformer on array', () => {
|
||||
mockTransformer.toModel.mockReturnValue( { toModel: 'Test' } );
|
||||
mocked( propertyTransformer.toModel ).mockReturnValue( { toModel: 'Test' } );
|
||||
|
||||
let transformed = transformation.toModel( { test: [ 'Test', 'Test2' ] } );
|
||||
|
||||
expect( transformed ).toMatchObject( { test: [ { toModel: 'Test' }, { toModel: 'Test' } ] } );
|
||||
expect( mockTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test' );
|
||||
expect( mockTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test2' );
|
||||
expect( propertyTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test' );
|
||||
expect( propertyTransformer.toModel ).toHaveBeenCalledWith( DummyModel, 'Test2' );
|
||||
|
||||
mockTransformer.fromModel.mockReturnValue( { fromModel: 'Test' } );
|
||||
mocked( propertyTransformer.fromModel ).mockReturnValue( { fromModel: 'Test' } );
|
||||
|
||||
transformed = transformation.fromModel( { test: [ 'Test', 'Test2' ] } );
|
||||
|
||||
expect( transformed ).toMatchObject( { test: [ { fromModel: 'Test' }, { fromModel: 'Test' } ] } );
|
||||
expect( mockTransformer.fromModel ).toHaveBeenCalledWith( 'Test' );
|
||||
expect( mockTransformer.fromModel ).toHaveBeenCalledWith( 'Test2' );
|
||||
expect( propertyTransformer.fromModel ).toHaveBeenCalledWith( 'Test' );
|
||||
expect( propertyTransformer.fromModel ).toHaveBeenCalledWith( 'Test2' );
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -1,56 +1,57 @@
|
|||
import * as moxios from 'moxios';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosClient } from '../axios-client';
|
||||
import { HTTPResponse } from '../../http-client';
|
||||
import { AxiosInterceptor } from '../axios-interceptor';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import axios from 'axios';
|
||||
|
||||
class DummyInterceptor extends AxiosInterceptor {
|
||||
public start = jest.fn();
|
||||
public stop = jest.fn();
|
||||
}
|
||||
|
||||
describe( 'AxiosClient', () => {
|
||||
let httpClient: AxiosClient;
|
||||
|
||||
beforeEach( () => {
|
||||
moxios.install();
|
||||
} );
|
||||
|
||||
afterEach( () => {
|
||||
moxios.uninstall();
|
||||
} );
|
||||
|
||||
it( 'should transform to HTTPResponse', async () => {
|
||||
const adapter = new MockAdapter( axios );
|
||||
|
||||
httpClient = new AxiosClient( { baseURL: 'http://test.test' } );
|
||||
|
||||
moxios.stubRequest( '/test', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseText: JSON.stringify( { test: 'value' } ),
|
||||
} );
|
||||
adapter
|
||||
.onGet( '/test' )
|
||||
.reply(
|
||||
200,
|
||||
{ test: 'value' },
|
||||
{ 'content-type': 'application/json' }
|
||||
);
|
||||
|
||||
const response = await httpClient.get( '/test' );
|
||||
|
||||
adapter.restore();
|
||||
|
||||
expect( response ).toBeInstanceOf( HTTPResponse );
|
||||
expect( response ).toHaveProperty( 'statusCode', 200 );
|
||||
expect( response ).toHaveProperty( 'headers', { 'content-type': 'application/json' } );
|
||||
expect( response ).toHaveProperty( 'headers', {
|
||||
'content-type': 'application/json',
|
||||
} );
|
||||
expect( response ).toHaveProperty( 'data', { test: 'value' } );
|
||||
} );
|
||||
|
||||
it( 'should start extra interceptors', async () => {
|
||||
const interceptor = mock< AxiosInterceptor >();
|
||||
const interceptor = new DummyInterceptor();
|
||||
|
||||
httpClient = new AxiosClient(
|
||||
{ baseURL: 'http://test.test' },
|
||||
[ interceptor ],
|
||||
);
|
||||
const adapter = new MockAdapter( axios );
|
||||
|
||||
moxios.stubRequest( '/test', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseText: JSON.stringify( { test: 'value' } ),
|
||||
} );
|
||||
httpClient = new AxiosClient( { baseURL: 'http://test.test' }, [
|
||||
interceptor,
|
||||
] );
|
||||
|
||||
adapter.onGet( '/test' ).reply( 200, { test: 'value' } );
|
||||
|
||||
await httpClient.get( '/test' );
|
||||
|
||||
adapter.restore();
|
||||
|
||||
expect( interceptor.start ).toHaveBeenCalled();
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import axios, { AxiosInstance } from 'axios';
|
||||
import * as moxios from 'moxios';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosInterceptor } from '../axios-interceptor';
|
||||
|
||||
class TestInterceptor extends AxiosInterceptor {}
|
||||
|
@ -7,10 +7,11 @@ class TestInterceptor extends AxiosInterceptor {}
|
|||
describe( 'AxiosInterceptor', () => {
|
||||
let interceptors: TestInterceptor[];
|
||||
let axiosInstance: AxiosInstance;
|
||||
let adapter: MockAdapter;
|
||||
|
||||
beforeEach( () => {
|
||||
axiosInstance = axios.create();
|
||||
moxios.install( axiosInstance );
|
||||
adapter = new MockAdapter( axiosInstance );
|
||||
interceptors = [];
|
||||
} );
|
||||
|
||||
|
@ -18,11 +19,11 @@ describe( 'AxiosInterceptor', () => {
|
|||
for ( const interceptor of interceptors ) {
|
||||
interceptor.stop( axiosInstance );
|
||||
}
|
||||
moxios.uninstall( axiosInstance );
|
||||
adapter.restore();
|
||||
} );
|
||||
|
||||
it( 'should not break interceptor chaining for success', async () => {
|
||||
moxios.stubRequest( 'http://test.test', { status: 200 } );
|
||||
adapter.onGet( 'http://test.test' ).reply( 200 );
|
||||
|
||||
interceptors.push( new TestInterceptor() );
|
||||
interceptors.push( new TestInterceptor() );
|
||||
|
@ -37,7 +38,7 @@ describe( 'AxiosInterceptor', () => {
|
|||
} );
|
||||
|
||||
it( 'should not break interceptor chaining for errors', async () => {
|
||||
moxios.stubRequest( 'http://test.test', { status: 401 } );
|
||||
adapter.onGet( 'http://test.test' ).reply( 401 );
|
||||
|
||||
interceptors.push( new TestInterceptor() );
|
||||
interceptors.push( new TestInterceptor() );
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
import axios, { AxiosInstance } from 'axios';
|
||||
import * as moxios from 'moxios';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosOAuthInterceptor } from '../axios-oauth-interceptor';
|
||||
|
||||
describe( 'AxiosOAuthInterceptor', () => {
|
||||
let apiAuthInterceptor: AxiosOAuthInterceptor;
|
||||
let axiosInstance: AxiosInstance;
|
||||
let adapter: MockAdapter;
|
||||
|
||||
beforeEach( () => {
|
||||
axiosInstance = axios.create();
|
||||
moxios.install( axiosInstance );
|
||||
adapter = new MockAdapter( axiosInstance );
|
||||
apiAuthInterceptor = new AxiosOAuthInterceptor(
|
||||
'consumer_key',
|
||||
'consumer_secret',
|
||||
|
@ -18,66 +19,40 @@ describe( 'AxiosOAuthInterceptor', () => {
|
|||
|
||||
afterEach( () => {
|
||||
apiAuthInterceptor.stop( axiosInstance );
|
||||
moxios.uninstall( axiosInstance );
|
||||
} );
|
||||
|
||||
it( 'should not run unless started', async () => {
|
||||
moxios.stubRequest( 'https://api.test', { status: 200 } );
|
||||
|
||||
apiAuthInterceptor.stop( axiosInstance );
|
||||
await axiosInstance.get( 'https://api.test' );
|
||||
|
||||
let request = moxios.requests.mostRecent();
|
||||
expect( request.headers ).not.toHaveProperty( 'Authorization' );
|
||||
|
||||
apiAuthInterceptor.start( axiosInstance );
|
||||
await axiosInstance.get( 'https://api.test' );
|
||||
|
||||
request = moxios.requests.mostRecent();
|
||||
expect( request.headers ).toHaveProperty( 'Authorization' );
|
||||
adapter.restore();
|
||||
} );
|
||||
|
||||
it( 'should use basic auth for HTTPS', async () => {
|
||||
moxios.stubRequest( 'https://api.test', { status: 200 } );
|
||||
await axiosInstance.get( 'https://api.test' );
|
||||
adapter.onGet( 'https://api.test' ).reply( 200 );
|
||||
const response = await axiosInstance.get( 'https://api.test' );
|
||||
|
||||
const request = moxios.requests.mostRecent();
|
||||
|
||||
expect( request.headers ).toHaveProperty( 'Authorization' );
|
||||
expect( request.headers.Authorization ).toBe(
|
||||
'Basic ' +
|
||||
Buffer.from( 'consumer_key:consumer_secret' ).toString( 'base64' ),
|
||||
);
|
||||
expect( response.config.auth ).not.toBeNull();
|
||||
expect( response.config.auth!.username ).toBe( 'consumer_key' );
|
||||
expect( response.config.auth!.password ).toBe( 'consumer_secret' );
|
||||
} );
|
||||
|
||||
it( 'should use OAuth 1.0a for HTTP', async () => {
|
||||
moxios.stubRequest( 'http://api.test', { status: 200 } );
|
||||
await axiosInstance.get( 'http://api.test' );
|
||||
|
||||
const request = moxios.requests.mostRecent();
|
||||
adapter.onGet( 'http://api.test' ).reply( 200 );
|
||||
const response = await axiosInstance.get( 'http://api.test' );
|
||||
|
||||
// We're going to assume that the oauth-1.0a package added the signature data correctly so we will
|
||||
// focus on ensuring that the header looks roughly correct given what we readily know.
|
||||
expect( request.headers ).toHaveProperty( 'Authorization' );
|
||||
expect( request.headers.Authorization ).toMatch(
|
||||
expect( response.config.headers! ).toHaveProperty( 'Authorization' );
|
||||
expect( response.config.headers!.Authorization ).toMatch(
|
||||
/^OAuth oauth_consumer_key="consumer_key".*oauth_signature_method="HMAC-SHA256".*oauth_version="1.0"/,
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'should work with base URL', async () => {
|
||||
moxios.stubRequest( '/test', { status: 200 } );
|
||||
await axiosInstance.request( {
|
||||
adapter.onGet( 'https://api.test/test' ).reply( 200 );
|
||||
const response = await axiosInstance.request( {
|
||||
method: 'GET',
|
||||
baseURL: 'https://api.test/',
|
||||
url: '/test',
|
||||
} );
|
||||
|
||||
const request = moxios.requests.mostRecent();
|
||||
|
||||
expect( request.headers ).toHaveProperty( 'Authorization' );
|
||||
expect( request.headers.Authorization ).toBe(
|
||||
'Basic ' +
|
||||
Buffer.from( 'consumer_key:consumer_secret' ).toString( 'base64' ),
|
||||
);
|
||||
expect( response.config.auth ).not.toBeNull();
|
||||
expect( response.config.auth!.username ).toBe( 'consumer_key' );
|
||||
expect( response.config.auth!.password ).toBe( 'consumer_secret' );
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -1,31 +1,30 @@
|
|||
import axios, { AxiosInstance } from 'axios';
|
||||
import * as moxios from 'moxios';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosResponseInterceptor } from '../axios-response-interceptor';
|
||||
|
||||
describe( 'AxiosResponseInterceptor', () => {
|
||||
let apiResponseInterceptor: AxiosResponseInterceptor;
|
||||
let axiosInstance: AxiosInstance;
|
||||
let adapter: MockAdapter;
|
||||
|
||||
beforeEach( () => {
|
||||
axiosInstance = axios.create();
|
||||
moxios.install( axiosInstance );
|
||||
adapter = new MockAdapter( axiosInstance );
|
||||
apiResponseInterceptor = new AxiosResponseInterceptor();
|
||||
apiResponseInterceptor.start( axiosInstance );
|
||||
} );
|
||||
|
||||
afterEach( () => {
|
||||
apiResponseInterceptor.stop( axiosInstance );
|
||||
moxios.uninstall();
|
||||
adapter.restore();
|
||||
} );
|
||||
|
||||
it( 'should transform responses into an HTTPResponse', async () => {
|
||||
moxios.stubRequest( 'http://test.test', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseText: JSON.stringify( { test: 'value' } ),
|
||||
} );
|
||||
adapter.onGet( 'http://test.test' ).reply(
|
||||
200,
|
||||
{ test: 'value' },
|
||||
{ 'content-type': 'application/json' }
|
||||
);
|
||||
|
||||
const response = await axiosInstance.get( 'http://test.test' );
|
||||
|
||||
|
@ -41,13 +40,11 @@ describe( 'AxiosResponseInterceptor', () => {
|
|||
} );
|
||||
|
||||
it( 'should transform error responses into an HTTPResponse', async () => {
|
||||
moxios.stubRequest( 'http://test.test', {
|
||||
status: 404,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseText: JSON.stringify( { code: 'error_code', message: 'value' } ),
|
||||
} );
|
||||
adapter.onGet( 'http://test.test' ).reply(
|
||||
404,
|
||||
{ code: 'error_code', message: 'value' },
|
||||
{ 'content-type': 'application/json' }
|
||||
);
|
||||
|
||||
await expect( axiosInstance.get( 'http://test.test' ) ).rejects.toMatchObject( {
|
||||
statusCode: 404,
|
||||
|
@ -62,7 +59,7 @@ describe( 'AxiosResponseInterceptor', () => {
|
|||
} );
|
||||
|
||||
it( 'should bubble non-response errors', async () => {
|
||||
moxios.stubTimeout( 'http://test.test' );
|
||||
adapter.onGet( 'http://test.test' ).timeout();
|
||||
|
||||
await expect( axiosInstance.get( 'http://test.test' ) ).rejects.toMatchObject(
|
||||
new Error( 'timeout of 0ms exceeded' ),
|
||||
|
|
|
@ -1,31 +1,33 @@
|
|||
import axios, { AxiosInstance } from 'axios';
|
||||
import * as moxios from 'moxios';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosURLToQueryInterceptor } from '../axios-url-to-query-interceptor';
|
||||
|
||||
describe( 'AxiosURLToQueryInterceptor', () => {
|
||||
let urlToQueryInterceptor: AxiosURLToQueryInterceptor;
|
||||
let axiosInstance: AxiosInstance;
|
||||
let adapter: MockAdapter;
|
||||
|
||||
beforeEach( () => {
|
||||
axiosInstance = axios.create();
|
||||
moxios.install( axiosInstance );
|
||||
adapter = new MockAdapter( axiosInstance );
|
||||
urlToQueryInterceptor = new AxiosURLToQueryInterceptor( 'test' );
|
||||
urlToQueryInterceptor.start( axiosInstance );
|
||||
} );
|
||||
|
||||
afterEach( () => {
|
||||
urlToQueryInterceptor.stop( axiosInstance );
|
||||
moxios.uninstall();
|
||||
adapter.restore();
|
||||
} );
|
||||
|
||||
it( 'should put path in query string', async () => {
|
||||
moxios.stubRequest( 'http://test.test/?test=%2Ftest%2Froute', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseText: JSON.stringify( { test: 'value' } ),
|
||||
} );
|
||||
adapter.onGet(
|
||||
'http://test.test/',
|
||||
{ params: { test: '/test/route' } }
|
||||
).reply(
|
||||
200,
|
||||
{ test: 'value' },
|
||||
{ 'content-type': 'application/json' }
|
||||
);
|
||||
|
||||
const response = await axiosInstance.get( 'http://test.test/test/route' );
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { AxiosRequestConfig } from 'axios';
|
||||
import * as createHmac from 'create-hmac';
|
||||
import * as OAuth from 'oauth-1.0a';
|
||||
import createHmac from 'create-hmac';
|
||||
import OAuth from 'oauth-1.0a';
|
||||
import { AxiosInterceptor } from './axios-interceptor';
|
||||
import { buildURLWithParams } from './utils';
|
||||
|
||||
|
@ -50,7 +50,7 @@ export class AxiosOAuthInterceptor extends AxiosInterceptor {
|
|||
username: this.oauth.consumer.key,
|
||||
password: this.oauth.consumer.secret,
|
||||
};
|
||||
} else {
|
||||
} else if ( request.headers ) {
|
||||
request.headers.Authorization = this.oauth.toHeader(
|
||||
this.oauth.authorize( {
|
||||
url,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { mock, MockProxy } from 'jest-mock-extended';
|
||||
import { mocked } from 'ts-jest/utils';
|
||||
import { HTTPClient, HTTPResponse } from '../../../http';
|
||||
import { ModelTransformer, ModelRepositoryParams } from '../../../framework';
|
||||
import { DummyModel } from '../../../__test_data__/dummy-model';
|
||||
|
@ -26,17 +26,25 @@ class DummyChildModel extends Model {
|
|||
}
|
||||
type DummyChildParams = ModelRepositoryParams< DummyChildModel, { parent: string }, { childSearch: string }, 'childName' >
|
||||
|
||||
jest.mock( '../../../framework/model-transformer' );
|
||||
|
||||
describe( 'Shared REST Functions', () => {
|
||||
let mockClient: MockProxy< HTTPClient >;
|
||||
let mockTransformer: MockProxy< ModelTransformer< any > > & ModelTransformer< any >;
|
||||
let mockClient: HTTPClient;
|
||||
let mockTransformer: ModelTransformer< any >;
|
||||
|
||||
beforeEach( () => {
|
||||
mockClient = mock< HTTPClient >();
|
||||
mockTransformer = mock< ModelTransformer< any > >();
|
||||
mockClient = {
|
||||
get: jest.fn(),
|
||||
post: jest.fn(),
|
||||
patch: jest.fn(),
|
||||
put: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
};
|
||||
mockTransformer = new ModelTransformer( [] );
|
||||
} );
|
||||
|
||||
it( 'restList', async () => {
|
||||
mockClient.get.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.get ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
[
|
||||
|
@ -50,7 +58,7 @@ describe( 'Shared REST Functions', () => {
|
|||
},
|
||||
],
|
||||
) );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restList< DummyModelParams >( () => 'test-url', DummyModel, mockClient, mockTransformer );
|
||||
|
||||
|
@ -65,7 +73,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restListChildren', async () => {
|
||||
mockClient.get.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.get ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
[
|
||||
|
@ -79,7 +87,7 @@ describe( 'Shared REST Functions', () => {
|
|||
},
|
||||
],
|
||||
) );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restListChild< DummyChildParams >(
|
||||
( parent ) => 'test-url-' + parent.parent,
|
||||
|
@ -99,7 +107,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restCreate', async () => {
|
||||
mockClient.post.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.post ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
{
|
||||
|
@ -107,8 +115,8 @@ describe( 'Shared REST Functions', () => {
|
|||
label: 'Test 1',
|
||||
},
|
||||
) );
|
||||
mockTransformer.fromModel.mockReturnValue( { name: 'From-Test' } );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.fromModel ).mockReturnValue( { name: 'From-Test' } );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restCreate< DummyModelParams >(
|
||||
( properties ) => 'test-url-' + properties.name,
|
||||
|
@ -126,7 +134,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restRead', async () => {
|
||||
mockClient.get.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.get ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
{
|
||||
|
@ -134,7 +142,7 @@ describe( 'Shared REST Functions', () => {
|
|||
label: 'Test 1',
|
||||
},
|
||||
) );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restRead< DummyModelParams >( ( id ) => 'test-url-' + id, DummyModel, mockClient, mockTransformer );
|
||||
|
||||
|
@ -146,7 +154,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restReadChildren', async () => {
|
||||
mockClient.get.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.get ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
{
|
||||
|
@ -154,7 +162,7 @@ describe( 'Shared REST Functions', () => {
|
|||
label: 'Test 1',
|
||||
},
|
||||
) );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restReadChild< DummyChildParams >(
|
||||
( parent, id ) => 'test-url-' + parent.parent + '-' + id,
|
||||
|
@ -171,7 +179,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restUpdate', async () => {
|
||||
mockClient.patch.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.patch ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
{
|
||||
|
@ -179,8 +187,8 @@ describe( 'Shared REST Functions', () => {
|
|||
label: 'Test 1',
|
||||
},
|
||||
) );
|
||||
mockTransformer.fromModel.mockReturnValue( { name: 'From-Test' } );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.fromModel ).mockReturnValue( { name: 'From-Test' } );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restUpdate< DummyModelParams >( ( id ) => 'test-url-' + id, DummyModel, mockClient, mockTransformer );
|
||||
|
||||
|
@ -193,7 +201,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restUpdateChildren', async () => {
|
||||
mockClient.patch.mockResolvedValue( new HTTPResponse(
|
||||
mocked( mockClient.patch ).mockResolvedValue( new HTTPResponse(
|
||||
200,
|
||||
{},
|
||||
{
|
||||
|
@ -201,8 +209,8 @@ describe( 'Shared REST Functions', () => {
|
|||
label: 'Test 1',
|
||||
},
|
||||
) );
|
||||
mockTransformer.fromModel.mockReturnValue( { name: 'From-Test' } );
|
||||
mockTransformer.toModel.mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
mocked( mockTransformer.fromModel ).mockReturnValue( { name: 'From-Test' } );
|
||||
mocked( mockTransformer.toModel ).mockReturnValue( new DummyChildModel( { name: 'Test' } ) );
|
||||
|
||||
const fn = restUpdateChild< DummyChildParams >(
|
||||
( parent, id ) => 'test-url-' + parent.parent + '-' + id,
|
||||
|
@ -220,7 +228,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restDelete', async () => {
|
||||
mockClient.delete.mockResolvedValue( new HTTPResponse( 200, {}, {} ) );
|
||||
mocked( mockClient.delete ).mockResolvedValue( new HTTPResponse( 200, {}, {} ) );
|
||||
|
||||
const fn = restDelete< DummyModelParams >( ( id ) => 'test-url-' + id, mockClient );
|
||||
|
||||
|
@ -231,7 +239,7 @@ describe( 'Shared REST Functions', () => {
|
|||
} );
|
||||
|
||||
it( 'restDeleteChildren', async () => {
|
||||
mockClient.delete.mockResolvedValue( new HTTPResponse( 200, {}, {} ) );
|
||||
mocked( mockClient.delete ).mockResolvedValue( new HTTPResponse( 200, {}, {} ) );
|
||||
|
||||
const fn = restDeleteChild< DummyChildParams >(
|
||||
( parent, id ) => 'test-url-' + parent.parent + '-' + id,
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import { mock, MockProxy } from 'jest-mock-extended';
|
||||
import { UpdatesSettings } from '../../models';
|
||||
import { SettingService } from '../setting-service';
|
||||
|
||||
describe( 'SettingService', () => {
|
||||
let repository: MockProxy< UpdatesSettings >;
|
||||
let repository: UpdatesSettings;
|
||||
let service: SettingService;
|
||||
|
||||
beforeEach( () => {
|
||||
repository = mock< UpdatesSettings >();
|
||||
repository = {
|
||||
update: jest.fn(),
|
||||
};
|
||||
service = new SettingService( repository );
|
||||
} );
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"types": [ "node", "jest", "axios", "moxios", "create-hmac" ],
|
||||
"rootDir": "src",
|
||||
"types": [ "node", "jest", "axios", "create-hmac" ],
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"target": "es5"
|
||||
},
|
||||
|
|
3344
pnpm-lock.yaml
3344
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
|
@ -1,32 +1,40 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"jsx": "preserve",
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"incremental": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
// Target the latest version of ECMAScript.
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
|
||||
// Support incremental project builds in the compiler.
|
||||
"composite": true,
|
||||
"emitDeclarationOnly": false,
|
||||
"isolatedModules": true,
|
||||
"incremental": true,
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true,
|
||||
|
||||
/* Additional Checks */
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
|
||||
/* Module Resolution Options */
|
||||
// Search under node_modules for non-relative imports.
|
||||
"moduleResolution": "node",
|
||||
|
||||
/* This needs to be false so our types are possible to consume without setting this */
|
||||
"esModuleInterop": false,
|
||||
"resolveJsonModule": true
|
||||
}
|
||||
// Support vanilla JavaScript and type-check it.
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
|
||||
// Ensures all modules are compatible with ES6 import syntax.
|
||||
"esModuleInterop": true,
|
||||
|
||||
// Allow JSON files to be imported as objects.
|
||||
"resolveJsonModule": true,
|
||||
|
||||
// Support mapping transpiled files back to source files.
|
||||
"sourceMap": true,
|
||||
"declarationMap": true,
|
||||
|
||||
// Enable all of the strict type checks.
|
||||
"strict": true,
|
||||
|
||||
// Warn developers when they write code incompatible with some build tools.
|
||||
"isolatedModules": true
|
||||
},
|
||||
// Make sure we don't transpile any test files.
|
||||
"exclude": [
|
||||
"**/__test_data__/*",
|
||||
"**/__tests__/*",
|
||||
"**/__mocks__/*",
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue