Skip to content

Commit

Permalink
Refactored getUniqueValues unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanaem committed Oct 12, 2023
1 parent 96dcf63 commit b53198b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
65 changes: 38 additions & 27 deletions cypress/unit/store-getter-getUniqueValues.cy.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
import { getters } from "~/store";

let store;
let state;

describe("getUniqueValues getter", () => {

beforeEach(() => {
state = {

store = {
columnToCategoryMap: {
column1: "Diagnosis",
column2: "Age",
column3: "Sex"
},

state: {
dataDictionary: {

columnToCategoryMap: {
annotated: {

column1: "Diagnosis",
column2: "Age",
column3: "Sex"
},
column1: { missingValues: [] },
column2: { missingValues: [] },
column3: { missingValues: ["column3_value1", "column3_value2"] }
}
},

dataDictionary: {
dataTable: [

annotated: {
{ column1: 1, column2: "column2_value1", column3: "column3_value1" },
{ column1: 2, column2: "column2_value2", column3: "column3_value2" },
{ column1: 1, column2: "column2_value3", column3: "column3_value3" },
{ column1: 3, column2: "column2_value2", column3: "column3_value4" }
]
};

column1: { missingValues: [] },
column2: { missingValues: [] },
column3: { missingValues: ["column3_value1", "column3_value2"] }
}
},
store = {
getters: {
// getColumnsForCategory: () => (p_category) => {
// return ["column1", "column2", "column3"];
// },
getColumnsForCategory: getters.getColumnsForCategory(state),

dataTable: [
getUniqueColumnValues: getters.getUniqueColumnValues(state)
},

{ column1: 1, column2: "column2_value1", column3: "column3_value1" },
{ column1: 2, column2: "column2_value2", column3: "column3_value2" },
{ column1: 1, column2: "column2_value3", column3: "column3_value3" },
{ column1: 3, column2: "column2_value2", column3: "column3_value4" }
]
}
};
});
state: state
};
}
);

it("Retrieves unique values of a single column", () => {

// Act
const diagnosisUniqueValues = getters.getUniqueValues(store.state)("Diagnosis");
const diagnosisUniqueValues = getters.getUniqueValues(store.state, store.getters)("Diagnosis");

// Assert
expect(diagnosisUniqueValues).to.deep.equal({ "column1": [1, 2, 3] });
Expand All @@ -50,10 +60,11 @@ describe("getUniqueValues getter", () => {
it("Retrieves unique values of a multiple columns", () => {

// Setup
// We change the state by reference because the state passed to the getters needs to reflect that change
store.state.columnToCategoryMap.column2 = "Diagnosis";

// Act
const diagnosisUniqueValues = getters.getUniqueValues(store.state)("Diagnosis");
const diagnosisUniqueValues = getters.getUniqueValues(store.state, store.getters)("Diagnosis");

// Assert
expect(diagnosisUniqueValues).to.deep.equal({
Expand All @@ -66,7 +77,7 @@ describe("getUniqueValues getter", () => {
it("Retrieve unique value list of maximum length", () => {

// Act
const diagnosisUniqueValues = getters.getUniqueValues(store.state)("Diagnosis", 2);
const diagnosisUniqueValues = getters.getUniqueValues(store.state, store.getters)("Diagnosis", 2);

// Assert
expect(diagnosisUniqueValues).to.deep.equal({ "column1": [1, 2]});
Expand All @@ -75,7 +86,7 @@ describe("getUniqueValues getter", () => {
it("Make sure no missing values are included in unique values", () => {

// Act
const diagnosisUniqueValues = getters.getUniqueValues(store.state)("Sex");
const diagnosisUniqueValues = getters.getUniqueValues(store.state, store.getters)("Sex");

// Assert
expect(diagnosisUniqueValues).to.deep.equal({ "column3": ["column3_value3", "column3_value4"]});
Expand Down
4 changes: 2 additions & 2 deletions store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ export const getters = {
return columns;
},

getUniqueColumnValues: (p_state, p_getters) => (columnName, p_maxValues="None") => {
getUniqueColumnValues: (p_state) => (columnName, p_maxValues="None") => {
let uniqueColumnValues = new Set();
for ( let index = 0; index < p_state.dataTable.length; index++ ) {

Expand Down Expand Up @@ -591,7 +591,7 @@ export const getters = {
const columns = p_getters.getColumnsForCategory(p_category);
const uniqueValues = {};
for (const column of columns) {
uniqueValues[column] = p_getters.getUniqueColumnValues(column);
uniqueValues[column] = p_getters.getUniqueColumnValues(column, p_maxValues);
}
return uniqueValues;
},
Expand Down

0 comments on commit b53198b

Please sign in to comment.