Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: fix not preferring floats over ints in some situations #5654

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/gatsby/src/schema/__tests__/data-tree-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,39 @@ describe(`Gatsby data tree utils`, () => {
example = getExampleValues([{ foo: [() => {}] }])
expect(example.foo).not.toBeDefined()
})

it(`prefers float when multiple number types`, () => {
let example

// nodes starting with integer
example = getExampleValues({ nodes: [{ number: 5 }, { number: 2.5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)

// with node not containing number field
example = getExampleValues({ nodes: [{ number: 5 }, {}, { number: 2.5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)

// nodes starting with float
example = getExampleValues({ nodes: [{ number: 2.5 }, { number: 5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)

// array of numbers - starting with integer
example = getExampleValues({ nodes: [{ numbers: [2.5, 5] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(2.5)

// array of numbers - starting with float
example = getExampleValues({ nodes: [{ numbers: [5, 2.5] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(2.5)
})
})

describe(`Type conflicts`, () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/data-tree-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const getExampleScalarFromArray = values =>
values,
(value, nextValue) => {
// Prefer floats over ints as they're more specific.
if (value && _.isNumber(value) && !_.isInteger(value)) {
return value
if (nextValue && _.isNumber(nextValue) && !_.isInteger(nextValue)) {
return nextValue
} else if (value === null) {
return nextValue
} else {
Expand Down