forked from tektoncd/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect Tasks container to redux store (tektoncd#64)
- Loading branch information
1 parent
e098866
commit 8a0d432
Showing
8 changed files
with
314 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2019 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { getTasks } from '../api'; | ||
|
||
export function fetchTasksSuccess(data) { | ||
return { | ||
type: 'TASKS_FETCH_SUCCESS', | ||
data | ||
}; | ||
} | ||
|
||
export function fetchTasks() { | ||
return async dispatch => { | ||
dispatch({ type: 'TASKS_FETCH_REQUEST' }); | ||
let tasks; | ||
try { | ||
tasks = await getTasks(); | ||
dispatch(fetchTasksSuccess(tasks)); | ||
} catch (error) { | ||
dispatch({ type: 'TASKS_FETCH_FAILURE', error }); | ||
} | ||
return tasks; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2019 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import configureStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
|
||
import * as API from '../api'; | ||
import { fetchTasks, fetchTasksSuccess } from './tasks'; | ||
|
||
it('fetchTasksSuccess', () => { | ||
const data = { fake: 'data' }; | ||
expect(fetchTasksSuccess(data)).toEqual({ | ||
type: 'TASKS_FETCH_SUCCESS', | ||
data | ||
}); | ||
}); | ||
|
||
it('fetchTasks', async () => { | ||
const tasks = { fake: 'tasks' }; | ||
const middleware = [thunk]; | ||
const mockStore = configureStore(middleware); | ||
const store = mockStore(); | ||
|
||
jest.spyOn(API, 'getTasks').mockImplementation(() => tasks); | ||
|
||
const expectedActions = [ | ||
{ type: 'TASKS_FETCH_REQUEST' }, | ||
fetchTasksSuccess(tasks) | ||
]; | ||
|
||
await store.dispatch(fetchTasks()); | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
|
||
it('fetchTasks error', async () => { | ||
const middleware = [thunk]; | ||
const mockStore = configureStore(middleware); | ||
const store = mockStore(); | ||
|
||
const error = new Error(); | ||
|
||
jest.spyOn(API, 'getTasks').mockImplementation(() => { | ||
throw error; | ||
}); | ||
|
||
const expectedActions = [ | ||
{ type: 'TASKS_FETCH_REQUEST' }, | ||
{ type: 'TASKS_FETCH_FAILURE', error } | ||
]; | ||
|
||
await store.dispatch(fetchTasks()); | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2019 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { combineReducers } from 'redux'; | ||
import keyBy from 'lodash.keyby'; | ||
|
||
function byName(state = {}, action) { | ||
switch (action.type) { | ||
case 'TASKS_FETCH_SUCCESS': | ||
return keyBy(action.data, 'metadata.name'); | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
function isFetching(state = false, action) { | ||
switch (action.type) { | ||
case 'TASKS_FETCH_REQUEST': | ||
return true; | ||
case 'TASKS_FETCH_SUCCESS': | ||
case 'TASKS_FETCH_FAILURE': | ||
return false; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
function errorMessage(state = null, action) { | ||
switch (action.type) { | ||
case 'TASKS_FETCH_FAILURE': | ||
return action.error.message; | ||
case 'TASKS_FETCH_REQUEST': | ||
case 'TASKS_FETCH_SUCCESS': | ||
return null; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export default combineReducers({ | ||
byName, | ||
errorMessage, | ||
isFetching | ||
}); | ||
|
||
export function getTasks(state) { | ||
return Object.values(state.byName); | ||
} | ||
|
||
export function getTask(state, name) { | ||
return state.byName[name]; | ||
} | ||
|
||
export function getTasksErrorMessage(state) { | ||
return state.errorMessage; | ||
} | ||
|
||
export function isFetchingTasks(state) { | ||
return state.isFetching; | ||
} |
Oops, something went wrong.