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

feat(gatsby): Remove boundActionCreators #29129

Merged
merged 14 commits into from
Feb 11, 2021
98 changes: 46 additions & 52 deletions packages/gatsby-plugin-sharp/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock(`async/queue`, () => () => {
})
jest.mock(`gatsby/dist/redux/actions`, () => {
return {
boundActionCreators: {
actions: {
createJobV2: jest.fn().mockReturnValue(Promise.resolve()),
},
}
Expand All @@ -30,7 +30,7 @@ const {
getImageSize,
getImageSizeAsync,
stats,
setBoundActionCreators,
setActions,
} = require(`../`)

const {
Expand Down Expand Up @@ -73,15 +73,13 @@ describe(`gatsby-plugin-sharp`, () => {
describe(`queueImageResizing`, () => {
;[`createJob`, `createJobV2`].forEach(api => {
describe(`with ${api}`, () => {
let boundActionCreators
let actions
beforeEach(() => {
boundActionCreators = {}
actions = {}
if (api === `createJobV2`) {
boundActionCreators.createJobV2 = jest
.fn()
.mockReturnValue(Promise.resolve())
actions.createJobV2 = jest.fn().mockReturnValue(Promise.resolve())
}
setBoundActionCreators(boundActionCreators)
setActions(actions)
scheduleJob.mockClear()
})

Expand All @@ -96,8 +94,8 @@ describe(`gatsby-plugin-sharp`, () => {
// We expect value to be rounded to 1
expect(result.height).toBe(1)
if (api === `createJobV2`) {
expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toMatchSnapshot()
} else {
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(scheduleJob).toMatchSnapshot()
Expand Down Expand Up @@ -126,8 +124,8 @@ describe(`gatsby-plugin-sharp`, () => {
expect(testName.match(/[!@#$^&," ]/)).not.toBe(false)
expect(queueResultName.match(/[!@#$^&," ]/)).not.toBe(true)
if (api === `createJobV2`) {
expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toMatchSnapshot()
} else {
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(scheduleJob).toMatchSnapshot()
Expand All @@ -150,61 +148,59 @@ describe(`gatsby-plugin-sharp`, () => {

it(`should return the same result when using createJob as createJobV2`, async () => {
scheduleJob.mockClear()
const boundActionCreators = {
const actions = {
createJobV2: jest.fn(() => Promise.resolve()),
}
setBoundActionCreators(boundActionCreators)
setActions(actions)
const resultV2 = await queueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 3 },
})

setBoundActionCreators({})
setActions({})
const result = await queueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 3 },
})
expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(result).toStrictEqual(resultV2)
})
})

describe(`fluid`, () => {
let boundActionCreators = {}
let actions = {}
beforeEach(() => {
boundActionCreators.createJobV2 = jest
.fn()
.mockReturnValue(Promise.resolve())
setBoundActionCreators(boundActionCreators)
actions.createJobV2 = jest.fn().mockReturnValue(Promise.resolve())
setActions(actions)
scheduleJob.mockClear()
})

it(`includes responsive image properties, e.g. sizes, srcset, etc.`, async () => {
const result = await fluid({ file })

expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(result).toMatchSnapshot()
})

it(`includes responsive image properties, e.g. sizes, srcset, etc. with the createJob api`, async () => {
setBoundActionCreators({})
setActions({})
const result = await fluid({ file })

expect(boundActionCreators.createJobV2).not.toHaveBeenCalled()
expect(actions.createJobV2).not.toHaveBeenCalled()
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(result).toMatchSnapshot()
})

it(`should give the same result with createJob as with createJobV2`, async () => {
const resultV2 = await fluid({ file })

setBoundActionCreators({})
setActions({})
const result = await fluid({ file })
expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(result).toStrictEqual(resultV2)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`adds pathPrefix if defined`, async () => {
Expand All @@ -218,7 +214,7 @@ describe(`gatsby-plugin-sharp`, () => {

expect(result.src.indexOf(pathPrefix)).toBe(0)
expect(result.srcSet.indexOf(pathPrefix)).toBe(0)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`keeps original file name`, async () => {
Expand All @@ -228,7 +224,7 @@ describe(`gatsby-plugin-sharp`, () => {

expect(path.parse(result.src).name).toBe(file.name)
expect(path.parse(result.srcSet).name).toBe(file.name)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`does not change the arguments object it is given`, async () => {
Expand All @@ -239,7 +235,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

expect(args).toEqual({ maxWidth: 400 })
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`infers the maxWidth if only maxHeight is given`, async () => {
Expand All @@ -250,7 +246,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

expect(result.presentationWidth).toEqual(41)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`calculate height based on width when maxWidth & maxHeight are present`, async () => {
Expand Down Expand Up @@ -287,13 +283,13 @@ describe(`gatsby-plugin-sharp`, () => {
)

for (const testCase of testsCases) {
boundActionCreators.createJobV2.mockClear()
actions.createJobV2.mockClear()
const result = await fluid({
file: fileObject,
args: testCase.args,
})

expect(boundActionCreators.createJobV2.mock.calls).toMatchSnapshot()
expect(actions.createJobV2.mock.calls).toMatchSnapshot()
expect(result.presentationWidth).toEqual(testCase.result[0])
expect(result.presentationHeight).toEqual(testCase.result[1])
}
Expand All @@ -307,7 +303,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

await expect(result).rejects.toThrow()
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`accepts srcSet breakpoints`, async () => {
Expand All @@ -327,7 +323,7 @@ describe(`gatsby-plugin-sharp`, () => {
const actual = findAllBreakpoints(result.srcSet)
// should contain all requested sizes as well as the original size
expect(actual).toEqual(expect.arrayContaining(expected))
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`should throw on srcSet breakpoints less than 1`, async () => {
Expand All @@ -339,7 +335,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

await expect(result).rejects.toThrow()
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`ensure maxWidth is in srcSet breakpoints`, async () => {
Expand All @@ -355,7 +351,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

expect(result.srcSet).toEqual(expect.stringContaining(`${maxWidth}w`))
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`reject any breakpoints larger than the original width`, async () => {
Expand Down Expand Up @@ -390,7 +386,7 @@ describe(`gatsby-plugin-sharp`, () => {
expect(actual).toEqual(expect.arrayContaining(expected))
// should contain no other sizes
expect(actual.length).toEqual(expected.length)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`prevents duplicate breakpoints`, async () => {
Expand All @@ -411,34 +407,32 @@ describe(`gatsby-plugin-sharp`, () => {
const actual = findAllBreakpoints(result.srcSet)
expect(actual).toEqual(expect.arrayContaining(expected))
expect(actual.length).toEqual(expected.length)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})
})

describe(`fixed`, () => {
let boundActionCreators = {}
let actions = {}
beforeEach(() => {
boundActionCreators.createJobV2 = jest
.fn()
.mockReturnValue(Promise.resolve())
setBoundActionCreators(boundActionCreators)
actions.createJobV2 = jest.fn().mockReturnValue(Promise.resolve())
setActions(actions)
scheduleJob.mockClear()
console.warn = jest.fn()
})

it(`should give the same result with createJob as with createJobV2`, async () => {
const boundActionCreators = {
const actions = {
createJobV2: jest.fn(() => Promise.resolve()),
}
setBoundActionCreators(boundActionCreators)
setActions(actions)
const resultV2 = await fixed({ file, args: { width: 1 } })

setBoundActionCreators({})
setActions({})
const result = await fixed({ file, args: { width: 1 } })
expect(boundActionCreators.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(scheduleJob).toHaveBeenCalledTimes(1)
expect(result).toStrictEqual(resultV2)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`does not warn when the requested width is equal to the image width`, async () => {
Expand All @@ -451,7 +445,7 @@ describe(`gatsby-plugin-sharp`, () => {

expect(result.width).toEqual(1)
expect(console.warn).toHaveBeenCalledTimes(0)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`warns when the requested width is greater than the image width`, async () => {
Expand All @@ -465,7 +459,7 @@ describe(`gatsby-plugin-sharp`, () => {

expect(result.width).toEqual(width)
expect(console.warn).toHaveBeenCalledTimes(1)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})

it(`correctly infers the width when only the height is given`, async () => {
Expand All @@ -477,7 +471,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

expect(result.width).toEqual(21)
expect(boundActionCreators.createJobV2).toMatchSnapshot()
expect(actions.createJobV2).toMatchSnapshot()
})
})

Expand Down
16 changes: 8 additions & 8 deletions packages/gatsby-plugin-sharp/src/__tests__/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const fs = require(`fs-extra`)
const workerMock = require(`../gatsby-worker`).IMAGE_PROCESSING

describe(`scheduler`, () => {
let boundActionCreators = {}
let actions = {}
let scheduler
beforeEach(() => {
workerMock.mockReset()
boundActionCreators = {
actions = {
createJob: jest.fn(),
endJob: jest.fn(),
}
Expand Down Expand Up @@ -43,7 +43,7 @@ describe(`scheduler`, () => {
pluginOptions: {},
},
}
await scheduleJob(job, boundActionCreators)
await scheduleJob(job, actions)

expect(workerMock).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(`scheduler`, () => {
pluginOptions: {},
},
},
boundActionCreators
actions
)
} catch (err) {
expect(err).toEqual(`failed transform`)
Expand All @@ -104,7 +104,7 @@ describe(`scheduler`, () => {
pluginOptions: {},
},
}
await scheduleJob(job, boundActionCreators)
await scheduleJob(job, actions)

expect(workerMock).not.toHaveBeenCalled()
fs.existsSync = orignalSync
Expand All @@ -126,8 +126,8 @@ describe(`scheduler`, () => {
pluginOptions: {},
},
}
const jobPromise = scheduleJob(job, boundActionCreators)
const job2Promise = scheduleJob(job, boundActionCreators)
const jobPromise = scheduleJob(job, actions)
const job2Promise = scheduleJob(job, actions)
expect(jobPromise).toStrictEqual(job2Promise)

await Promise.all([jobPromise, job2Promise])
Expand Down Expand Up @@ -165,7 +165,7 @@ describe(`scheduler`, () => {
},
}

await scheduleJob(job, boundActionCreators)
await scheduleJob(job, actions)

expect(got.post).toHaveBeenCalledWith(
process.env.GATSBY_CLOUD_IMAGE_SERVICE_URL,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
setBoundActionCreators,
setActions,
// queue: jobQueue,
// reportError,
_unstable_createJob,
Expand Down Expand Up @@ -123,7 +123,7 @@ exports.onPreBootstrap = async (
{ actions, emitter, reporter, cache, store },
pluginOptions
) => {
setBoundActionCreators(actions)
setActions(actions)
setPluginOptions(pluginOptions)

// below is a hack / hot fix for confusing progress bar behaviour
Expand Down
Loading