add core/bindings store tests

This commit is contained in:
Damián Suárez 2024-03-27 13:17:01 +00:00
parent ebc95b5d0b
commit d9b81fc1d6
3 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,30 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { ACTION_REGISTER_BINDINGS_SOURCE } from '../constants';
import { registerSourceHandler } from '../actions';
describe( 'actions', () => {
describe( 'registerSourceHandler', () => {
it( 'should return the ACTION_REGISTER_BINDINGS_SOURCE action', () => {
const source = {
name: 'namespace/source-name',
label: 'The name source handler of the namespace',
anotherAttribute: 'anotherValue',
};
deepFreeze( source );
const result = registerSourceHandler( source );
expect( result ).toEqual( {
type: ACTION_REGISTER_BINDINGS_SOURCE,
...source,
} );
} );
} );
} );

View File

@ -0,0 +1,106 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import reducer from '../reducer';
import { ACTION_REGISTER_BINDINGS_SOURCE } from '../constants';
describe( 'reducer', () => {
describe( 'ACTION_REGISTER_BINDINGS_SOURCE', () => {
it( 'should handle the action and update the state accordingly', () => {
const initialState = {
sources: {},
};
deepFreeze( initialState );
const source = {
name: 'namespace/source-name',
label: 'Source handler of the namespace',
anotherAttribute: 'anotherValue',
};
const action = {
type: ACTION_REGISTER_BINDINGS_SOURCE,
...source,
};
deepFreeze( action );
const expectedState = {
sources: {
'namespace/source-name': {
label: 'Source handler of the namespace',
anotherAttribute: 'anotherValue',
},
},
};
const resultState = reducer( initialState, action );
expect( resultState ).toEqual( expectedState );
} );
it( 'should add new sources without overwriting existing ones', () => {
const initialState = {
sources: {
'namespace/old-source-name': {
label: 'An old source',
anotherAttribute: 'someValue',
},
},
};
deepFreeze( initialState );
const newSource = {
name: 'namespace/new-source-name',
label: 'A new source',
newAttribute: 'newValue',
};
const action = {
type: ACTION_REGISTER_BINDINGS_SOURCE,
...newSource,
};
deepFreeze( action );
const expectedState = {
...initialState,
sources: {
...initialState.sources,
'namespace/new-source-name': {
label: 'A new source',
newAttribute: 'newValue',
},
},
};
const resultState = reducer( initialState, action );
expect( resultState ).toEqual( expectedState );
} );
it( 'should not modify state if action type does not match', () => {
const initialState = {
sources: {
'namespace/old-source-name': {
label: 'An old source',
anotherAttribute: 'someValue',
},
},
};
deepFreeze( initialState );
const action = {
type: 'UNKNOWN_ACTION',
name: 'namespace/new-source',
};
deepFreeze( action );
const resultState = reducer( initialState, action );
expect( resultState ).toEqual( initialState );
} );
} );
} );

View File

@ -0,0 +1,88 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { getAllBindingsSources, getBindingsSource } from '../selectors';
describe( 'selectors', () => {
describe( 'getAllBindingsSources', () => {
it( 'should return all registered bindings sources', () => {
const state = {
sources: {
'namespace/source-name_1': {
name: 'namespace/source-name_1',
label: 'The first source handler of the namespace',
anotherAttribute: 'anotherValue',
},
'namespace/source-name_2': {
name: 'namespace/source-name_2',
label: 'The second source handler of the namespace',
anotherAttribute: 'anotherValue',
},
},
};
deepFreeze( state );
const result = getAllBindingsSources( state );
expect( result ).toEqual( state.sources );
} );
} );
describe( 'getBindingsSource', () => {
it( 'should return a specific bindings source given its name', () => {
const state = {
sources: {
'namespace/source-name_1': {
name: 'namespace/source-name_1',
label: 'The first source handler of the namespace',
anotherAttribute: 'anotherValue',
},
'namespace/source-name_2': {
name: 'namespace/source-name_2',
label: 'The second source handler of the namespace',
anotherAttribute: 'anotherValue',
},
},
};
deepFreeze( state );
const result = getBindingsSource(
state,
'namespace/source-name_2'
);
expect( result ).toEqual(
state.sources[ 'namespace/source-name_2' ]
);
} );
it( 'should return undefined if the bindings source does not exist', () => {
const state = {
sources: {
sources: {
'namespace/source-name_1': {
name: 'namespace/source-name_1',
label: 'The first source handler of the namespace',
anotherAttribute: 'anotherValue',
},
'namespace/source-name_2': {
name: 'namespace/source-name_2',
label: 'The second source handler of the namespace',
anotherAttribute: 'anotherValue',
},
},
},
};
deepFreeze( state );
const result = getBindingsSource(
state,
'namespace/source-name_3'
);
expect( result ).toBeUndefined();
} );
} );
} );