Commit 72ed09b6 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch '39498-part-1' into 'master'

!21542 Part 1: Add new getters for Web IDE store

See merge request gitlab-org/gitlab!21673
parents 6d42e04c a783153c
......@@ -115,5 +115,30 @@ export const isOnDefaultBranch = (_state, getters) =>
export const canPushToBranch = (_state, getters) =>
getters.currentBranch && getters.currentBranch.can_push;
export const isFileDeletedAndReadded = (state, getters) => path => {
const stagedFile = getters.getStagedFile(path);
const file = state.entries[path];
return Boolean(stagedFile && stagedFile.deleted && file.tempFile);
};
// checks if any diff exists in the staged or unstaged changes for this path
export const getDiffInfo = (state, getters) => path => {
const stagedFile = getters.getStagedFile(path);
const file = state.entries[path];
const renamed = file.prevPath ? file.path !== file.prevPath : false;
const deletedAndReadded = getters.isFileDeletedAndReadded(path);
const deleted = deletedAndReadded ? false : file.deleted;
const tempFile = deletedAndReadded ? false : file.tempFile;
const changed = file.content !== (deletedAndReadded ? stagedFile.raw : file.raw);
return {
exists: changed || renamed || deleted || tempFile,
changed,
renamed,
deleted,
tempFile,
};
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
---
title: "!21542 Part 1: Add new utils for Web IDE store"
merge_request: 21673
author:
type: fixed
import * as getters from '~/ide/stores/getters';
import state from '~/ide/stores/state';
import { createStore } from '~/ide/stores';
import { file } from '../helpers';
describe('IDE store getters', () => {
let localState;
let localStore;
beforeEach(() => {
localState = state();
localStore = createStore();
localState = localStore.state;
});
describe('activeFile', () => {
......@@ -310,4 +312,90 @@ describe('IDE store getters', () => {
expect(getters.canPushToBranch({}, localGetters)).toBeFalsy();
});
});
describe('isFileDeletedAndReadded', () => {
const f = { ...file('sample'), content: 'sample', raw: 'sample' };
it.each([
{
entry: { ...f, tempFile: true },
staged: { ...f, deleted: true },
output: true,
},
{
entry: { ...f, content: 'changed' },
staged: { ...f, content: 'changed' },
output: false,
},
{
entry: { ...f, content: 'changed' },
output: false,
},
])(
'checks staged and unstaged files to see if a file was deleted and readded (case %#)',
({ entry, staged, output }) => {
Object.assign(localState, {
entries: {
[entry.path]: entry,
},
stagedFiles: [],
});
if (staged) localState.stagedFiles.push(staged);
expect(localStore.getters.isFileDeletedAndReadded(entry.path)).toBe(output);
},
);
});
describe('getDiffInfo', () => {
const f = { ...file('sample'), content: 'sample', raw: 'sample' };
it.each([
{
entry: { ...f, tempFile: true },
staged: { ...f, deleted: true },
output: { deleted: false, changed: false, tempFile: false },
},
{
entry: { ...f, tempFile: true, content: 'changed', raw: '' },
staged: { ...f, deleted: true },
output: { deleted: false, changed: true, tempFile: false },
},
{
entry: { ...f, content: 'changed' },
output: { changed: true },
},
{
entry: { ...f, content: 'sample' },
staged: { ...f, content: 'changed' },
output: { changed: false },
},
{
entry: { ...f, deleted: true },
output: { deleted: true, changed: false },
},
{
entry: { ...f, prevPath: 'old_path' },
output: { renamed: true, changed: false },
},
{
entry: { ...f, prevPath: 'old_path', content: 'changed' },
output: { renamed: true, changed: true },
},
])(
'compares changes in a file entry and returns a resulting diff info (case %#)',
({ entry, staged, output }) => {
Object.assign(localState, {
entries: {
[entry.path]: entry,
},
stagedFiles: [],
});
if (staged) localState.stagedFiles.push(staged);
expect(localStore.getters.getDiffInfo(entry.path)).toEqual(expect.objectContaining(output));
},
);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment