2019-10-19 16:38:07 +02:00
|
|
|
import fetchMock from 'fetch-mock';
|
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
|
|
|
|
import thunk from 'redux-thunk';
|
2019-03-11 14:48:48 +01:00
|
|
|
import {
|
|
|
|
|
FETCH_NAMESPACESTRATEGIES_TYPES,
|
|
|
|
|
FETCH_NAMESPACESTRATEGIES_TYPES_FAILURE,
|
|
|
|
|
FETCH_NAMESPACESTRATEGIES_TYPES_PENDING,
|
|
|
|
|
FETCH_NAMESPACESTRATEGIES_TYPES_SUCCESS,
|
|
|
|
|
fetchNamespaceStrategiesIfNeeded,
|
|
|
|
|
fetchNamespaceStrategiesSuccess,
|
|
|
|
|
shouldFetchNamespaceStrategies,
|
|
|
|
|
default as reducer,
|
|
|
|
|
getNamespaceStrategies,
|
|
|
|
|
isFetchNamespaceStrategiesPending,
|
2019-10-19 16:38:07 +02:00
|
|
|
getFetchNamespaceStrategiesFailure,
|
|
|
|
|
} from './namespaceStrategies';
|
|
|
|
|
import { MODIFY_CONFIG_SUCCESS } from './config';
|
2019-03-11 14:48:48 +01:00
|
|
|
|
|
|
|
|
const strategies = {
|
2019-10-19 16:38:07 +02:00
|
|
|
current: 'UsernameNamespaceStrategy',
|
2019-03-11 14:48:48 +01:00
|
|
|
available: [
|
2019-10-19 16:38:07 +02:00
|
|
|
'UsernameNamespaceStrategy',
|
|
|
|
|
'CustomNamespaceStrategy',
|
|
|
|
|
'CurrentYearNamespaceStrategy',
|
|
|
|
|
'RepositoryTypeNamespaceStrategy',
|
2019-03-11 14:48:48 +01:00
|
|
|
],
|
|
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-19 16:38:07 +02:00
|
|
|
href: 'http://localhost:8081/scm/api/v2/namespaceStrategies',
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
describe('namespace strategy caching', () => {
|
|
|
|
|
it('should fetch strategies, on empty state', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(shouldFetchNamespaceStrategies({})).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should fetch strategies, on empty namespaceStrategies node', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
2019-10-19 16:38:07 +02:00
|
|
|
namespaceStrategies: {},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(shouldFetchNamespaceStrategies(state)).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should not fetch strategies, on pending state', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
|
|
|
|
pending: {
|
2019-10-19 16:38:07 +02:00
|
|
|
[FETCH_NAMESPACESTRATEGIES_TYPES]: true,
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(shouldFetchNamespaceStrategies(state)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should not fetch strategies, on failure state', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
|
|
|
|
failure: {
|
2019-10-19 16:38:07 +02:00
|
|
|
[FETCH_NAMESPACESTRATEGIES_TYPES]: new Error('no...'),
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(shouldFetchNamespaceStrategies(state)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should not fetch strategies, if they are already fetched', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
|
|
|
|
namespaceStrategies: {
|
2019-10-19 16:38:07 +02:00
|
|
|
current: 'some',
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(shouldFetchNamespaceStrategies(state)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
describe('namespace strategies fetch', () => {
|
|
|
|
|
const URL = 'http://scm.hitchhiker.com/api/v2/namespaceStrategies';
|
2019-03-11 14:48:48 +01:00
|
|
|
const mockStore = configureMockStore([thunk]);
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createStore = (initialState = {}) => {
|
|
|
|
|
return mockStore({
|
|
|
|
|
...initialState,
|
|
|
|
|
indexResources: {
|
|
|
|
|
links: {
|
|
|
|
|
namespaceStrategies: {
|
2019-10-19 16:38:07 +02:00
|
|
|
href: URL,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should successfully fetch strategies', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
fetchMock.getOnce(URL, strategies);
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
2019-10-19 16:38:07 +02:00
|
|
|
{
|
|
|
|
|
type: FETCH_NAMESPACESTRATEGIES_TYPES_PENDING,
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
{
|
|
|
|
|
type: FETCH_NAMESPACESTRATEGIES_TYPES_SUCCESS,
|
2019-10-19 16:38:07 +02:00
|
|
|
payload: strategies,
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = createStore();
|
|
|
|
|
return store.dispatch(fetchNamespaceStrategiesIfNeeded()).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should dispatch FETCH_NAMESPACESTRATEGIES_TYPES_FAILURE on server error', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
fetchMock.getOnce(URL, {
|
2019-10-19 16:38:07 +02:00
|
|
|
status: 500,
|
2019-03-11 14:48:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const store = createStore();
|
|
|
|
|
return store.dispatch(fetchNamespaceStrategiesIfNeeded()).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toBe(FETCH_NAMESPACESTRATEGIES_TYPES_PENDING);
|
|
|
|
|
expect(actions[1].type).toBe(FETCH_NAMESPACESTRATEGIES_TYPES_FAILURE);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should not dispatch any action, if the strategies are already fetched', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const store = createStore({
|
2019-10-19 16:38:07 +02:00
|
|
|
namespaceStrategies: strategies,
|
2019-03-11 14:48:48 +01:00
|
|
|
});
|
|
|
|
|
store.dispatch(fetchNamespaceStrategiesIfNeeded());
|
|
|
|
|
expect(store.getActions().length).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
describe('namespace strategies reducer', () => {
|
|
|
|
|
it('should return unmodified state on unknown action', () => {
|
2019-03-11 16:39:08 +01:00
|
|
|
const state = {};
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(reducer(state)).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should store the strategies on success', () => {
|
2019-03-11 16:39:08 +01:00
|
|
|
const newState = reducer({}, fetchNamespaceStrategiesSuccess(strategies));
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(newState).toBe(strategies);
|
|
|
|
|
});
|
2019-03-11 16:39:08 +01:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should clear store if config was modified', () => {
|
2019-03-11 16:39:08 +01:00
|
|
|
const modifyConfigAction = {
|
|
|
|
|
type: MODIFY_CONFIG_SUCCESS,
|
|
|
|
|
payload: {
|
2019-10-19 16:38:07 +02:00
|
|
|
namespaceStrategy: 'CustomNamespaceStrategy',
|
|
|
|
|
},
|
2019-03-11 16:39:08 +01:00
|
|
|
};
|
|
|
|
|
const newState = reducer(strategies, modifyConfigAction);
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(newState.current).toEqual('CustomNamespaceStrategy');
|
2019-03-11 16:39:08 +01:00
|
|
|
});
|
2019-03-11 14:48:48 +01:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
describe('namespace strategy selectors', () => {
|
|
|
|
|
const error = new Error('The end of the universe');
|
2019-03-11 14:48:48 +01:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return an empty object', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(getNamespaceStrategies({})).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return the namespace strategies', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
2019-10-19 16:38:07 +02:00
|
|
|
namespaceStrategies: strategies,
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(getNamespaceStrategies(state)).toBe(strategies);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return true, when fetch namespace strategies is pending', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
|
|
|
|
pending: {
|
2019-10-19 16:38:07 +02:00
|
|
|
[FETCH_NAMESPACESTRATEGIES_TYPES]: true,
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(isFetchNamespaceStrategiesPending(state)).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return false, when fetch strategies is not pending', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(isFetchNamespaceStrategiesPending({})).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return error when fetch namespace strategies did fail', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
const state = {
|
|
|
|
|
failure: {
|
2019-10-19 16:38:07 +02:00
|
|
|
[FETCH_NAMESPACESTRATEGIES_TYPES]: error,
|
|
|
|
|
},
|
2019-03-11 14:48:48 +01:00
|
|
|
};
|
|
|
|
|
expect(getFetchNamespaceStrategiesFailure(state)).toEqual(error);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should return undefined when fetch strategies did not fail', () => {
|
2019-03-11 14:48:48 +01:00
|
|
|
expect(getFetchNamespaceStrategiesFailure({})).toBe(undefined);
|
|
|
|
|
});
|
|
|
|
|
});
|