Skip to content
Snippets Groups Projects
Commit 50e998fa authored by Atte Keinänen's avatar Atte Keinänen
Browse files

Fix isCardDirty and add tests for it and serializeCardForUrl

parent eb9da7cc
Branches
Tags
No related merge requests found
......@@ -56,7 +56,7 @@ export function isCardDirty(card, originalCard) {
}
} else {
const origCardSerialized = originalCard ? serializeCardForUrl(originalCard) : null;
const newCardSerialized = card ? serializeCardForUrl(card) : null;
const newCardSerialized = card ? serializeCardForUrl(_.omit(card, 'original_card_id')) : null;
return (newCardSerialized !== origCardSerialized);
}
}
......
import { isCardDirty, serializeCardForUrl, deserializeCardFromUrl } from "./card";
const CARD_ID = 31;
// TODO Atte Keinänen 8/5/17: Create a reusable version `getCard` for reducing test code duplication
const getCard = ({
newCard = false,
hasOriginalCard = false,
isNative = false,
database = 1,
display = "table",
queryFields = {},
table = undefined,
}) => {
const savedCardFields = {
name: "Example Saved Question",
description: "For satisfying your craving for information",
created_at: "2017-04-20T16:52:55.353Z",
id: CARD_ID
};
return {
"name": null,
"display": display,
"visualization_settings": {},
"dataset_query": {
"database": database,
"type": isNative ? "native" : "query",
...(!isNative ? {
query: {
...(table ? {"source_table": table} : {}),
...queryFields
}
} : {}),
...(isNative ? {
native: { query: "SELECT * FROM ORDERS"}
} : {})
},
...(newCard ? {} : savedCardFields),
...(hasOriginalCard ? {"original_card_id": CARD_ID} : {})
};
};
describe("browser", () => {
describe("isCardDirty", () => {
it("should consider a new card clean if no db table or native query is defined", () => {
expect(isCardDirty(
getCard({newCard: true}),
null
)).toBe(false);
});
it("should consider a new card dirty if a db table is chosen", () => {
expect(isCardDirty(
getCard({newCard: true, table: 5}),
null
)).toBe(true);
});
it("should consider a new card dirty if there is any content on the native query", () => {
expect(isCardDirty(
getCard({newCard: true, table: 5}),
null
)).toBe(true);
});
it("should consider a saved card and a matching original card identical", () => {
expect(isCardDirty(
getCard({hasOriginalCard: true}),
getCard({hasOriginalCard: false})
)).toBe(false);
});
it("should consider a saved card dirty if the current card doesn't match the last saved version", () => {
expect(isCardDirty(
getCard({hasOriginalCard: true, queryFields: [["field-id", 21]]}),
getCard({hasOriginalCard: false})
)).toBe(true);
});
});
describe("serializeCardForUrl", () => {
it("should include `original_card_id` property to the serialized URL", () => {
const cardAfterSerialization =
deserializeCardFromUrl(serializeCardForUrl(getCard({hasOriginalCard: true})));
expect(cardAfterSerialization).toHaveProperty("original_card_id", CARD_ID)
})
})
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment