first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
export {
attachMock,
attachCache,
addMock,
removeMock,
restoreFetch,
emptyFetch,
} from './setup-fetch';

View File

@@ -0,0 +1,9 @@
export default class MockDataBase {
constructor( addMockCallback ) {
addMockCallback( this.getType(), this.getCommand(), () => Promise.resolve( this.getMockData() ) );
}
getType() {}
getCommand() {}
getMockData() {}
}

View File

@@ -0,0 +1,5 @@
{
"id": "4521fd0",
"title": "test",
"value": "red"
}

View File

@@ -0,0 +1,16 @@
import MockDataBase from 'elementor/tests/qunit/mock/e-data/mock/base/mock-data-base';
import Data from './4521fd0.json';
export class GlobalsColorsCreate extends MockDataBase {
getType() {
return 'create';
}
getCommand() {
return 'globals/colors';
}
getMockData() {
return Data;
}
}

View File

@@ -0,0 +1,18 @@
import MockDataBase from 'elementor/tests/qunit/mock/e-data/mock/base/mock-data-base';
import Data from './4521fd0.json';
export class GlobalsColorsGet extends MockDataBase {
getType() {
return 'get';
}
getCommand() {
return 'globals/colors';
}
getMockData() {
return {
'4521fd0': Data,
};
}
}

View File

@@ -0,0 +1,16 @@
import MockDataBase from 'elementor/tests/qunit/mock/e-data/mock/base/mock-data-base';
import Data from './fcf2ddc.json';
export class GlobalsTypographyCreate extends MockDataBase {
getType() {
return 'create';
}
getCommand() {
return 'globals/typography';
}
getMockData() {
return Data;
}
}

View File

@@ -0,0 +1,8 @@
{
"id": "fcf2ddc",
"title": "test",
"value": {
"typography_typography": "custom",
"typography_font_family": "Arial"
}
}

View File

@@ -0,0 +1,18 @@
import MockDataBase from 'elementor/tests/qunit/mock/e-data/mock/base/mock-data-base';
import Data from './fcf2ddc.json';
export class GlobalsTypographyGet extends MockDataBase {
getType() {
return 'get';
}
getCommand() {
return 'globals/typography';
}
getMockData() {
return {
fcf2ddc: Data,
};
}
}

View File

@@ -0,0 +1,4 @@
export { GlobalsColorsCreate } from './globals/colors/create';
export { GlobalsColorsGet } from './globals/colors/get';
export { GlobalsTypographyCreate } from './globals/typography/create';
export { GlobalsTypographyGet } from './globals/typography/get';

View File

@@ -0,0 +1,98 @@
import * as mockClasses from './mock/';
// eslint-disable-next-line no-unused-vars
/* global wpApiSettings */
window.wpApiSettings = {
nonce: 'test_nonce',
};
export const fetchOriginal = $e.data.fetch;
/**
* @type {[].<{ type, command, callback }>}
*/
export let mockData = [];
/**
* @param {DataTypes} type
* @param {string} command
* @param {function(result, RequestData)|null} [callback=null]
*/
export const addMock = ( /* */ type, command, callback = null ) => {
if ( mockData.find( ( mock ) => mock.type === type && mock.command === command ) ) {
throw Error( `Mock type: '${ type }', command: '${ command }' is already exist` );
}
// Default callback return query and data merged.
if ( ! callback ) {
callback = ( result, requestData ) => {
const { query } = requestData.args,
{ data } = requestData.args;
return { ... query, ... data };
};
}
return mockData.push( { type, command, callback } );
};
export const removeMock = ( type, command ) => {
let result = false;
mockData = mockData.filter( ( mock ) => {
if ( type === mock.type && command === mock.command ) {
result = mock;
return false;
}
return true;
} );
return result;
};
export const attachMock = () => {
$e.data.fetch = ( /* RequestData */ requestData, fetchAPI ) => {
if ( fetchAPI ) {
return fetchOriginal( requestData, fetchAPI );
}
let result;
mockData.some( ( mockObject ) => {
if ( mockObject.type === requestData.type && mockObject.command === requestData.command ) {
result = mockObject.callback( result, requestData );
return true;
}
} );
if ( undefined !== result ) {
result = Promise.resolve( result );
}
return result;
};
};
export const attachCache = () => {
$e.data.fetch = ( /* RequestData */ requestData, fetchAPI ) => {
if ( fetchAPI ) {
return fetchOriginal( requestData, fetchAPI );
}
return $e.data.cache.getAsync( requestData );
};
};
export const restoreFetch = () => {
$e.data.fetch = fetchOriginal;
};
export const emptyFetch = () => {
$e.data.fetch = () => Promise.resolve( {} );
};
// Initial mock.
Object.values( mockClasses ).forEach( ( MockClass ) => new MockClass( addMock ) );