Skip to content

Commit

Permalink
test(protocol-designer): mock file-saver while using cypress (#5943)
Browse files Browse the repository at this point in the history
Closes #5457

* sets chrome version to `stable` in travis (it was using some older version)
  • Loading branch information
IanLondon authored Jun 22, 2020
1 parent 21aa0ed commit 3f1c7f6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# opentrons platform travis config
cache: false

addons:
chrome: stable

stages:
- test
- name: app
Expand Down
41 changes: 26 additions & 15 deletions protocol-designer/cypress/integration/migrations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,35 @@ describe('Protocol fixtures migrate and match snapshots', () => {
.click()
}

cy.window().then(window => {
const savedFile = cloneDeep(window.__lastSavedFile__)
const expected = cloneDeep(expectedExportProtocol)
cy.window()
.its('__lastSavedFileBlob__')
.should('be.a', 'blob')
.should(async blob => {
const blobText = await blob.text()
const savedFile = JSON.parse(blobText)
const expectedFile = cloneDeep(expectedExportProtocol)

assert.match(
savedFile.designerApplication.version,
/^4\.0\.\d+$/,
'designerApplication.version is 4.0.x'
)
;[savedFile, expected].forEach(f => {
// Homogenize fields we don't want to compare
f.metadata.lastModified = 123
f.designerApplication.data._internalAppBuildDate = 'Foo Date'
f.designerApplication.version = 'x.x.x'
assert.match(
savedFile.designerApplication.version,
/^4\.0\.\d+$/,
'designerApplication.version is 4.0.x'
)
;[savedFile, expectedFile].forEach(f => {
// Homogenize fields we don't want to compare
f.metadata.lastModified = 123
f.designerApplication.data._internalAppBuildDate = 'Foo Date'
f.designerApplication.version = 'x.x.x'
})

expectDeepEqual(assert, savedFile, expectedFile)
})

expectDeepEqual(assert, savedFile, expected)
})
cy.window()
.its('__lastSavedFileName__')
.should(
'equal',
`${expectedExportProtocol.metadata.protocolName}.json`
)
})
})
}
Expand Down
6 changes: 6 additions & 0 deletions protocol-designer/cypress/mocks/file-saver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// mock for 'file-saver' npm module

export const saveAs = (blob, fileName) => {
global.__lastSavedFileBlob__ = blob
global.__lastSavedFileName__ = fileName
}
8 changes: 1 addition & 7 deletions protocol-designer/src/components/FileSidebar/FileSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ const saveFile = (downloadData: $PropertyType<Props, 'downloadData'>) => {
const blob = new Blob([JSON.stringify(downloadData.fileData)], {
type: 'application/json',
})
if (process.env.CYPRESS === '1') {
// HACK(IL, 2020-04-02): can't figure out a better way to do this yet
// https://docs.cypress.io/faq/questions/using-cypress-faq.html#Can-my-tests-interact-with-Redux-Vuex-data-store
global.__lastSavedFile__ = downloadData.fileData
} else {
saveAs(blob, downloadData.fileName)
}
saveAs(blob, downloadData.fileName)
}

type WarningContent = {|
Expand Down
11 changes: 11 additions & 0 deletions protocol-designer/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ const envVars = passThruEnvVars.reduce(
{ ...envVarsWithDefaults }
)

const testAliases =
process.env.CYPRESS === '1'
? {
'file-saver': path.resolve(__dirname, 'cypress/mocks/file-saver.js'),
}
: {}

console.log(`PD version: ${OT_PD_VERSION || 'UNKNOWN!'}`)

module.exports = merge(baseConfig, {
Expand Down Expand Up @@ -78,4 +85,8 @@ module.exports = merge(baseConfig, {
}),
new ScriptExtHtmlWebpackPlugin({ defaultAttribute: 'defer' }),
],

resolve: {
alias: testAliases,
},
})

0 comments on commit 3f1c7f6

Please sign in to comment.