Skip to content

Commit

Permalink
refactor: migrate tests
Browse files Browse the repository at this point in the history
refs: #2318
  • Loading branch information
Torsten Knauf authored and Torsten Knauf committed Nov 9, 2021
1 parent dcfa015 commit d67620e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 160 deletions.
151 changes: 0 additions & 151 deletions visualization/app/codeCharta/state/nodeSearch.service.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { TEST_FILE_WITH_PATHS } from "../../../util/dataMocks"
import { getNodesByGitignorePath } from "./getNodesByGitignorePath"

describe("getNodesByGitignorePath", () => {
it("should retrieve no nodes for an empty search", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "")

expect(searchedNodes.length).toBe(0)
})

it("should retrieve no nodes if searched within nothing", () => {
const searchedNodes = getNodesByGitignorePath(undefined, "")

expect(searchedNodes.length).toBe(0)
})

it("should find nodes based on a wildcard search", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "*small*")

expect(searchedNodes.length).toBe(2)
expect(searchedNodes[0].path).toBe("/root/Parent Leaf/small leaf")
expect(searchedNodes[1].path).toBe("/root/Parent Leaf/other small leaf")
})

it("should find nodes based on string search", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "small")

expect(searchedNodes.length).toBe(2)
expect(searchedNodes[0].path).toBe("/root/Parent Leaf/small leaf")
expect(searchedNodes[1].path).toBe("/root/Parent Leaf/other small leaf")
})

it("should find no nodes based on prefix search with non existing prefix", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "oot/*")

expect(searchedNodes.length).toBe(0)
})

it("should find all nodes based on prefix search with prefix of root node", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "root*")

expect(searchedNodes.length).toBe(6)
})

it("should find nodes based on exact match", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, '"/root/Parent Leaf"')

expect(searchedNodes.length).toBe(4)
expect(searchedNodes[0].path).toBe("/root/Parent Leaf")
expect(searchedNodes[1].path).toBe("/root/Parent Leaf/small leaf")
expect(searchedNodes[2].path).toBe("/root/Parent Leaf/other small leaf")
expect(searchedNodes[3].path).toBe("/root/Parent Leaf/empty folder")
})

it("should not ignore a leading whitespace with exact match", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, '" /root/Parent Leaf"')

expect(searchedNodes.length).toBe(0)
})

it("should find nodes based on inverted search", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "!leaf")

expect(searchedNodes.length).toBe(1)
expect(searchedNodes[0].path).toBe("/root")
})

it("should find nodes based on multiple inverted search", () => {
const searchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, " \tsmall, \tbig")
expect(searchedNodes.length).toBe(3)
expect(searchedNodes[0].path).toBe("/root/big leaf")
expect(searchedNodes[1].path).toBe("/root/Parent Leaf/small leaf")
expect(searchedNodes[2].path).toBe("/root/Parent Leaf/other small leaf")

const invertedSearchedNodes = getNodesByGitignorePath(TEST_FILE_WITH_PATHS.map, "! \tsmall, \tbig")

expect(invertedSearchedNodes.length).toBe(3)
expect(invertedSearchedNodes[0].path).toBe("/root")
expect(invertedSearchedNodes[1].path).toBe("/root/Parent Leaf")
expect(invertedSearchedNodes[2].path).toBe("/root/Parent Leaf/empty folder")
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { returnIgnore, transformPath } from "../../../util/codeMapHelper"

export function getNodesByGitignorePath(root: CodeMapNode, gitignorePath: string) {
gitignorePath = gitignorePath.trimStart()
if (gitignorePath.length === 0) {
if (gitignorePath.length === 0 || !root) {
return []
}
const ignoreResults = returnIgnore(gitignorePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestBed } from "@angular/core/testing"
import { render, screen, fireEvent } from "@testing-library/angular"
import userEvent from "@testing-library/user-event"
import { setSearchedNodePaths } from "../../../state/store/dynamicSettings/searchedNodePaths/searchedNodePaths.actions"
import { Store } from "../../../state/store/store"

import { searchedNodePathsSelector } from "../../../state/selectors/searchedNodes/searchedNodePaths.selector"
import { Store } from "../../../state/store/store"
import { NodeContextMenuController } from "../../nodeContextMenu/nodeContextMenu.component"
import { MapTreeViewModule } from "../mapTreeView.module"
import { MapTreeViewLevelComponent } from "./mapTreeViewLevel.component"
Expand All @@ -15,6 +15,12 @@ jest.mock("../../nodeContextMenu/nodeContextMenu.component", () => ({
broadcastShowEvent: jest.fn()
}
}))
jest.mock("../../../state/selectors/searchedNodes/searchedNodePaths.selector", () => ({
searchedNodePathsSelector: jest.fn(
jest.requireActual("../../../state/selectors/searchedNodes/searchedNodePaths.selector").searchedNodePathsSelector
)
}))
const searchedNodePathsSelectorMock = searchedNodePathsSelector as unknown as jest.Mock<ReturnType<typeof searchedNodePathsSelector>>

describe("mapTreeViewLevel", () => {
const componentProperties = {
Expand Down Expand Up @@ -103,12 +109,12 @@ describe("mapTreeViewLevel", () => {
})

it("should make searched items 'angular-green'", async () => {
searchedNodePathsSelectorMock.mockImplementationOnce(() => new Set(["/root/bigLeaf"]))
const { container, detectChanges } = await render(MapTreeViewLevelComponent, {
componentProperties,
excludeComponentDeclaration: true
})

Store.store.dispatch(setSearchedNodePaths(new Set(["/root/bigLeaf"])))
detectChanges()

const bigLeaf = container.querySelector("#\\/root\\/bigLeaf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { BlacklistType, CodeMapNode, ColorMode, EdgeVisibility, NodeType, State
import { CODE_MAP_BUILDING, STATE } from "../../dataMocks"
import { HierarchyRectangularNode } from "d3-hierarchy"

jest.mock("../../../state/selectors/accumulatedData/unifiedMapNode.selector", () => ({
unifiedMapNodeSelector: () => ({
name: "Anode",
path: "/root/Anode",
type: "File",
attributes: { theHeight: 100 },
isExcluded: false,
isFlattened: false
})
}))

describe("TreeMapHelper", () => {
describe("build node", () => {
let codeMapNode: CodeMapNode
Expand Down Expand Up @@ -175,19 +186,16 @@ describe("TreeMapHelper", () => {
})

it("should not be a flat node, because its searched for", () => {
state.dynamicSettings.searchedNodePaths = new Set(["/root/Anode"])
state.dynamicSettings.searchPattern = "Anode"
state.dynamicSettings.searchPattern = "/root/Anode"
expect(buildNode().flat).toBeFalsy()
})

it("should be a flat node, because other nodes are searched for", () => {
state.dynamicSettings.searchedNodePaths = new Set(["/root/anotherNode", "/root/anotherNode2"])
state.dynamicSettings.searchPattern = "Anode"
state.dynamicSettings.searchPattern = "/root/anotherNode2"
expect(buildNode().flat).toBeTruthy()
})

it("should not be a flat node when searchPattern is empty", () => {
state.dynamicSettings.searchedNodePaths = new Set(["/root/anotherNode", "/root/anotherNode2"])
state.dynamicSettings.searchPattern = ""
expect(buildNode().flat).toBeFalsy()
})
Expand Down

0 comments on commit d67620e

Please sign in to comment.