[JENKINS-35831] implement logic for toggling favorite in store + tests

This commit is contained in:
Cliff Meyers 2016-07-11 15:21:25 -04:00
parent 6c1a967def
commit 43f0ed1ec8
4 changed files with 147 additions and 10 deletions

View File

@ -13,7 +13,7 @@ builder.defineTask('test', function() {
var mocha = require('gulp-mocha');
var babel = require('babel-core/register');
builder.gulp.src('src/test/js/**/*-spec.jsx')
builder.gulp.src('src/test/js/**/*-spec.{js,jsx}')
.pipe(mocha({
compilers: { js: babel },
})).on('error', function(e) {

View File

@ -73,6 +73,7 @@ export const actions = {
),
};
// TODO: need to validate that payload is passing through after JENKINS-36580 is complete
return dispatch(actions.generateData(
{ url, fetchOptions },
ACTION_TYPES.TOGGLE_FAVORITE,

View File

@ -10,7 +10,7 @@ import { User } from '../model/User';
/* eslint new-cap: [0] */
const { Record, List } = Immutable;
const FavoritesState = Record({
export const FavoritesState = Record({
user: null,
favorites: null,
});
@ -30,16 +30,21 @@ const actionHandlers = {
const favoriteList = new List(payload);
return state.set('favorites', favoriteList);
},
[ACTION_TYPES.TOGGLE_FAVORITE](state, { pipeline, favorite }) {
// TODO: handle add and remove of pipeline from List properly
if (pipeline) {
return state.set('favorites', []);
[ACTION_TYPES.TOGGLE_FAVORITE](state, { addFavorite, branchToRemove, payload }) {
const favoritesList = state.get('favorites');
if (addFavorite) {
const appendedList = favoritesList.push(payload);
return state.set('favorites', appendedList);
}
return {
pipeline,
favorite,
};
const toggledBranchHref = branchToRemove._links.self.href;
const prunedList = favoritesList.filter(fav => {
const favoritedBranch = fav.item;
return favoritedBranch._links.self.href !== toggledBranchHref;
});
return state.set('favorites', prunedList);
},
};

View File

@ -0,0 +1,131 @@
/**
* Created by cmeyers on 7/11/16.
*/
import { assert } from 'chai';
import Store from '../../../main/js/redux/FavoritesStore';
import { ACTION_TYPES, FavoritesState } from '../../../main/js/redux/FavoritesStore';
const getDefaultFavorites = () =>
JSON.parse(`
[
{
"_class": "io.jenkins.blueocean.service.embedded.rest.FavoriteImpl",
"_links": {
"self": {
"_class": "io.jenkins.blueocean.rest.hal.Link",
"href": "/blue/rest/users/cmeyers/favorites/blueocean%2FUX-301/"
}
},
"item": {
"_class": "io.jenkins.blueocean.service.embedded.rest.BranchImpl",
"_links": {
"self": {
"_class": "io.jenkins.blueocean.rest.hal.Link",
"href": "/blue/rest/organizations/jenkins/pipelines/blueocean/branches/UX-301/"
}
}
}
},
{
"_class": "io.jenkins.blueocean.service.embedded.rest.FavoriteImpl",
"_links": {
"self": {
"_class": "io.jenkins.blueocean.rest.hal.Link",
"href": "/blue/rest/users/cmeyers/favorites/jenkinsfile-experiments%2Fmaster/"
}
},
"item": {
"_class": "io.jenkins.blueocean.service.embedded.rest.BranchImpl",
"_links": {
"self": {
"_class": "io.jenkins.blueocean.rest.hal.Link",
"href": "/blue/rest/organizations/jenkins/pipelines/jenkinsfile-experiments/branches/master/"
}
}
}
}
]
`);
const createBranch = (selfHref) => {
return {
_links: {
self: {
href: selfHref,
},
},
};
};
describe('favoritesStore', () => {
const stateWithFavorites = Store.favoritesStore(
new FavoritesState(),
{
type: ACTION_TYPES.SET_FAVORITES,
payload: getDefaultFavorites(),
}
);
describe('SET_FAVORITES', () => {
it('stores the favorites correctly', () => {
assert.equal(stateWithFavorites.get('favorites').size, 2);
});
});
describe('TOGGLE_FAVORITE', () => {
it('removes an existing favorite when toggled', () => {
const newState = Store.favoritesStore(
stateWithFavorites,
{
type: ACTION_TYPES.TOGGLE_FAVORITE,
addFavorite: false,
branchToRemove: createBranch(
'/blue/rest/organizations/jenkins/pipelines/blueocean/branches/UX-301/'
),
payload: null,
}
);
const favorites = newState.get('favorites');
assert.isOk(favorites);
assert.equal(favorites.size, 1);
// ensure favorite is not present in list
for (const fav of favorites.values()) {
assert.notEqual(
'/blue/rest/organizations/jenkins/pipelines/blueocean/branches/UX-301/',
fav.item._links.self.href
);
}
});
it('adds a new favorite when toggled', () => {
const selfHref = '/blue/rest/organizations/jenkins/pipelines/test1/branches/master/';
const favoriteToAdd = {
item: {
_links: {
self: {
href: selfHref,
},
},
},
};
const newState = Store.favoritesStore(
stateWithFavorites,
{
type: ACTION_TYPES.TOGGLE_FAVORITE,
addFavorite: true,
branchToRemove: null,
payload: favoriteToAdd,
}
);
const favorites = newState.get('favorites');
assert.isOk(favorites);
assert.equal(favorites.size, 3);
assert.isOk(favorites.includes(favoriteToAdd));
});
});
});