From 79aa25f18e776d7ab8c44efbf15fb57a6f813c9e Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Sun, 19 Jul 2020 22:23:11 -0600 Subject: [PATCH] Add lib/utils/finite_state_machine to renamed diff viewer - Move the constants from a global diffs constants file into the more specific diff_viewers constants file - Create a file just for the Renamed Diff Viewer State Machine - Remove the state tests from the component and test the new Renamed-Diff-Viewer-specific state machine - Update the component to have a state machine attached instead of ad hoc is and transition methods --- .../components/diff_viewer/constants.js | 11 +++ .../diff_viewer/viewers/renamed.vue | 39 +++------ .../viewers/renamed/state_machine.js | 45 ++++++++++ .../viewers/renamed/state_machine_spec.js | 66 +++++++++++++++ .../diff_viewer/viewers/renamed_spec.js | 82 +++---------------- 5 files changed, 145 insertions(+), 98 deletions(-) create mode 100644 app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed/state_machine.js create mode 100644 spec/frontend/vue_shared/components/diff_viewer/viewers/renamed/state_machine_spec.js diff --git a/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js b/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js index 6c1840361af29f..97769e0d067040 100644 --- a/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js +++ b/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js @@ -10,3 +10,14 @@ export const imageViewMode = { swipe: 'swipe', onion: 'onion', }; + +// State machine states +export const RENAMED_STATE_IDLING = 'idle'; +export const RENAMED_STATE_LOADING = 'loading'; +export const RENAMED_STATE_ERRORED = 'errored'; + +// State machine transitions +export const RENAMED_TRANSITION_LOAD_START = 'LOAD_START'; +export const RENAMED_TRANSITION_LOAD_ERROR = 'LOAD_ERROR'; +export const RENAMED_TRANSITION_LOAD_SUCCEED = 'LOAD_SUCCEED'; +export const RENAMED_TRANSITION_ACKNOWLEDGE_ERROR = 'ACKNOWLEDGE_ERROR'; diff --git a/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue b/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue index b786f7752dfa7b..f9d86f4740530b 100644 --- a/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue +++ b/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue @@ -2,23 +2,23 @@ import { GlAlert, GlLink, GlLoadingIcon, GlSprintf } from '@gitlab/ui'; import { mapActions } from 'vuex'; +import { truncateSha } from '~/lib/utils/text_utility'; +import { __ } from '~/locale'; + import { TRANSITION_LOAD_START, TRANSITION_LOAD_ERROR, TRANSITION_LOAD_SUCCEED, TRANSITION_ACKNOWLEDGE_ERROR, - STATE_IDLING, STATE_LOADING, STATE_ERRORED, - RENAMED_DIFF_TRANSITIONS, -} from '~/diffs/constants'; -import { truncateSha } from '~/lib/utils/text_utility'; -import { __ } from '~/locale'; + getStateMachine, +} from './renamed/state_machine'; export default { STATE_LOADING, STATE_ERRORED, - TRANSITIONS: RENAMED_DIFF_TRANSITIONS, + stateMachine: getStateMachine(), uiText: { showLink: __('Show file contents'), commitLink: __('View file @ %{commitSha}'), @@ -37,11 +37,6 @@ export default { required: true, }, }, - data() { - return { - state: STATE_IDLING, - }; - }, computed: { shortSha() { return truncateSha(this.diffFile.content_sha); @@ -52,25 +47,15 @@ export default { }, methods: { ...mapActions('diffs', ['switchToFullDiffFromRenamedFile']), - transition(transitionEvent) { - const key = `${this.state}:${transitionEvent}`; - - if (this.$options.TRANSITIONS[key]) { - this.state = this.$options.TRANSITIONS[key]; - } - }, - is(state) { - return this.state === state; - }, switchToFull() { - this.transition(TRANSITION_LOAD_START); + this.$options.stateMachine.send(TRANSITION_LOAD_START); this.switchToFullDiffFromRenamedFile({ diffFile: this.diffFile }) .then(() => { - this.transition(TRANSITION_LOAD_SUCCEED); + this.$options.stateMachine.send(TRANSITION_LOAD_SUCCEED); }) .catch(() => { - this.transition(TRANSITION_LOAD_ERROR); + this.$options.stateMachine.send(TRANSITION_LOAD_ERROR); }); }, clickLink(event) { @@ -81,7 +66,7 @@ export default { } }, dismissError() { - this.transition(TRANSITION_ACKNOWLEDGE_ERROR); + this.$options.stateMachine.send(TRANSITION_ACKNOWLEDGE_ERROR); }, }, }; @@ -89,10 +74,10 @@ export default {