Skip to content

Instantly share code, notes, and snippets.

@wldcordeiro
Created October 20, 2017 03:54
Show Gist options
  • Save wldcordeiro/2cdd00730896736d387b15e50a08eb42 to your computer and use it in GitHub Desktop.
Save wldcordeiro/2cdd00730896736d387b15e50a08eb42 to your computer and use it in GitHub Desktop.
// I believe this is doable thanks to the transform-object-rest-spread Babel plugin.
export function formatConditionalAnimals({ bears, cats }) {
return {
animals: {
...(bears != null && { bears }),
...(cats != null && { cats }),
},
}
}
describe('formatConditionalAnimals', () => {
it('formats the bears only', () => {
const testBears = 3
expect(formatConditionalAnimals({ bears: testBears })).toMatchSnapshot()
})
it('formats the bears and cats', () => {
const testBears = 3
const testCats = 100
expect(
formatConditionalAnimals({
bears: testBears,
cats: testCats,
})
).toMatchSnapshot()
})
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`formatConditionalAnimals formats the bears and cats 1`] = `
Object {
"animals": Object {
"bears": 3,
"cats": 100,
},
}
`;
exports[`formatConditionalAnimals formats the bears only 1`] = `
Object {
"animals": Object {
"bears": 3,
},
}
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment