Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryLibrary model and reducer #1239

Merged
merged 4 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/js/state/QueryLibrary/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Group, Query, QueryLibraryAction, QueryLibraryState} from "./types"
import produce from "immer"
import {get, set, initial, last} from "lodash"
import {get, set, initial, last, isEqual} from "lodash"
import init from "./initial"

export default produce(
Expand Down Expand Up @@ -71,7 +71,7 @@ const moveItem = (
// cause an off by one issue since the destination index will be affected after
// removal (e.g. an item cannot be moved to the end of its current group because of this).
// For this situation we instead remove the item first, and then insert its copy
if (srcItemPath.length === destItemPath.length) {
if (isEqual(initial(srcItemPath), initial(destItemPath))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about that "initial" function. Very cool

removeItemFromGroup(draft, srcItemPath)
addItemToGroup(
draft,
Expand Down
24 changes: 19 additions & 5 deletions src/js/state/QueryLibrary/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const testLib = {
id: "testId2",
name: "testName2",
description: "testDescription2",
value: "testValue2",
zql: "testValue2",
tags: ["testTag1", "testTag2"]
},
{
Expand All @@ -36,7 +36,7 @@ const testLib = {
id: "testId4",
name: "testName4",
description: "testDescription4",
value: "testValue4",
zql: "testValue4",
tags: ["testTag2"]
}
]
Expand All @@ -46,7 +46,7 @@ const testLib = {
id: "testId5",
name: "testName5",
description: "testDescription5",
value: "testValue5",
zql: "testValue5",
tags: ["testTag1"]
}
]
Expand All @@ -58,7 +58,7 @@ const newQuery = {
id: "newQueryId",
name: "newQueryName",
description: "newQueryDescription",
value: "newQueryValue",
zql: "newQueryValue",
tags: []
}

Expand Down Expand Up @@ -138,7 +138,7 @@ test("remove query, group", () => {
expect(getGroup(store.getState(), [0]).items).toEqual([testName1Group[2]])
})

test("move query, same group", () => {
test("move query, same group, different group same depth", () => {
store.dispatch(QueryLibrary.setAll(testLib))

const testName1Group = getGroup(store.getState(), [0]).items
Expand All @@ -161,6 +161,20 @@ test("move query, same group", () => {

expect(getGroup(store.getState(), [0]).items).toHaveLength(3)
expect(getGroup(store.getState(), [0]).items).toEqual(testName1Group)

// move to "uncle's" group
store.dispatch(QueryLibrary.addItem(newGroup, [0]))

expect(getGroup(store.getState(), [0, 1]).items).toHaveLength(1)
expect(getGroup(store.getState(), [0, 3]).items).toHaveLength(0)

const testName4Query = getGroup(store.getState(), [0, 1]).items[0]

store.dispatch(QueryLibrary.moveItem([0, 1, 0], [0, 3, 0]))

expect(getGroup(store.getState(), [0, 1]).items).toHaveLength(0)
expect(getGroup(store.getState(), [0, 3]).items).toHaveLength(1)
expect(getGroup(store.getState(), [0, 3]).items[0]).toEqual(testName4Query)
})

test("move query, different group", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/js/state/QueryLibrary/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type QueryLibraryState = Group
export interface Query {
id: string
name: string
value: string
zql: string
description: string
tags: string[]
}
Expand Down