Skip to content

Commit

Permalink
Add test for applyColumnState
Browse files Browse the repository at this point in the history
  • Loading branch information
mschindlerMM committed Nov 2, 2023
1 parent 933660a commit 8717b5e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/AgGrid.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module AgGrid exposing
, GridConfig, grid
, defaultGridConfig, defaultSettings
, onCellChanged, onCellDoubleClicked, onSelectionChange, onContextMenu
, ColumnState, onColumnStateChanged, columnStatesDecoder, columnStatesEncoder
, ColumnState, onColumnStateChanged, columnStatesDecoder, columnStatesEncoder, applyColumnState
, FilterState, onFilterStateChanged, filterStatesEncoder, filterStatesDecoder
, Sidebar, SidebarType(..), SidebarPosition(..), defaultSidebar
, aggregationToString, pinningTypeToString, sortingToString, toAggregation, toPinningType, toSorting
Expand Down Expand Up @@ -36,7 +36,7 @@ module AgGrid exposing
# ColumnState
@docs ColumnState, onColumnStateChanged, columnStatesDecoder, columnStatesEncoder
@docs ColumnState, onColumnStateChanged, columnStatesDecoder, columnStatesEncoder, applyColumnState
# FilterState
Expand Down Expand Up @@ -718,6 +718,14 @@ grid gridConfig events columnDefs data =
[]


{-| Apply the column state from the `GridConfig` to the given `ColumnDefs`.
The values from the cache overwrite the values on the ColumnDef. The order is also according
to the order in the column state. New columns, that don't exist in the column state, are appended to the end.
**This function is mainly exposed to allow unit-testing, as this is automatically applied to ColumnDefs passed to the `grid`.**
-}
applyColumnState : GridConfig dataType -> List (ColumnDef dataType) -> List (ColumnDef dataType)
applyColumnState gridConfig columnDefs =
let
Expand Down
131 changes: 130 additions & 1 deletion tests/AgGridTest.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module AgGridTest exposing (suite)

import AgGrid exposing (Aggregation(..), PinningType(..), Sorting(..))
import AgGrid exposing (Aggregation(..), PinningType(..), Sorting(..), defaultGridConfig)
import Expect
import Html.Attributes exposing (default)
import Test exposing (..)


Expand Down Expand Up @@ -77,4 +78,132 @@ suite =
in
Expect.equalLists input result
]
, let
defaultSettings =
AgGrid.defaultSettings
|> (\settings ->
{ settings
| aggFunc = AgGrid.NoAggregation
, flex = Nothing
, hide = False
, pinned = AgGrid.Unpinned
, pivot = False
, pivotIndex = Nothing
, rowGroup = False
, rowGroupIndex = Nothing
, sort = AgGrid.NoSorting
, sortIndex = Nothing
, width = Just 100
}
)

defaultColumn =
{ field = "test"
, renderer = AgGrid.StringRenderer .foo
, headerName = "Test"
, settings = defaultSettings
}

defaultColumnState =
{ aggFunc = Nothing
, colId = "test"
, flex = Nothing
, hide = Nothing
, pinned = Nothing
, pivot = Nothing
, pivotIndex = Nothing
, rowGroup = Nothing
, rowGroupIndex = Nothing
, sort = Nothing
, sortIndex = Nothing
, width = 100
}
in
describe "applyColumnState"
[ test "should update ColumnDef according to the column state" <|
\_ ->
let
config =
{ defaultGridConfig
| columnStates =
[ { aggFunc = Just "avg"
, colId = "test"
, flex = Just 5
, hide = Just True
, pinned = Just "left"
, pivot = Just True
, pivotIndex = Just 1
, rowGroup = Just True
, rowGroupIndex = Just 2
, sort = Just "asc"
, sortIndex = Just 3
, width = 150
}
]
}

expected =
{ defaultColumn
| settings =
{ defaultSettings
| aggFunc = AgGrid.AvgAggregation
, flex = Just 5
, hide = True
, pinned = AgGrid.PinnedToLeft
, pivot = True
, pivotIndex = Just 1
, rowGroup = True
, rowGroupIndex = Just 2
, sort = AgGrid.SortAscending
, sortIndex = Just 3
, width = Just 150
}
}
in
AgGrid.applyColumnState config [ defaultColumn ]
|> Expect.equal [ expected ]
, test "should apply the order from the column state" <|
\_ ->
let
config =
{ defaultGridConfig
| columnStates =
[ { defaultColumnState | colId = "bazz" }
, { defaultColumnState | colId = "foo" }
, { defaultColumnState | colId = "bar" }
]
}
in
AgGrid.applyColumnState config
[ { defaultColumn | field = "foo" }
, { defaultColumn | field = "bar" }
, { defaultColumn | field = "bazz" }
]
|> Expect.equal
[ { defaultColumn | field = "bazz" }
, { defaultColumn | field = "foo" }
, { defaultColumn | field = "bar" }
]
, test "should append columns that are not in the state to the end" <|
\_ ->
let
config =
{ defaultGridConfig
| columnStates =
[ { defaultColumnState | colId = "foo" }
, { defaultColumnState | colId = "bar" }
]
}
in
AgGrid.applyColumnState config
[ { defaultColumn | field = "foo" }
, { defaultColumn | field = "bazz" }
, { defaultColumn | field = "bar" }
]
|> Expect.equal
[ { defaultColumn | field = "foo" }
, { defaultColumn | field = "bar" }
, { defaultColumn | field = "bazz" }
]
]
]

0 comments on commit 8717b5e

Please sign in to comment.