forked from folio-org/ui-eholdings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup Mirage in test and development environments
Autoload mirage modules based on directory structure
- Loading branch information
Showing
17 changed files
with
401 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"presets": [["env", { "modules": false }], "react"], | ||
"presets": [["env", { "modules": false }], "stage-3", "react"], | ||
"plugins": ["react-hot-loader/babel"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Mirage from 'mirage-server'; | ||
|
||
export default function () { | ||
this.namespace = 'eholdings'; | ||
|
||
this.get('/vendors', ({ vendors }, request) => { | ||
let filteredVendors = vendors.all().filter((vendorModel) => { | ||
return vendorModel.vendorName.includes(request.queryParams.search); | ||
}); | ||
|
||
return filteredVendors; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Factory, faker } from 'mirage-server'; | ||
|
||
export default Factory.extend({ | ||
vendorId: () => faker.random.number(), | ||
vendorName: () => faker.company.companyName(), | ||
vendorToken: null, | ||
isCustomer: false, | ||
packagesTotal: 1, | ||
packagesSelected: 0, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
let start = () => {}; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
start = require('./start').startDevMirage; | ||
} | ||
|
||
export default start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Model } from 'mirage-server'; | ||
|
||
export default Model.extend(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function(server) { | ||
server.createList('vendor', 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Serializer } from 'mirage-server'; | ||
|
||
export default Serializer.extend({ | ||
serialize(response, request) { | ||
let json = Serializer.prototype.serialize.apply(this, arguments); | ||
json.totalResults = json[this.keyForResource(response)].length; | ||
return json; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Mirage from 'mirage-server'; | ||
import baseConfig from './config'; | ||
import camelCase from 'lodash/camelCase'; | ||
import '../tests/force-fetch-polyfill'; | ||
|
||
const moduleTypes = ['factories', 'fixtures', 'scenarios', 'models', 'serializers', 'identity-managers']; | ||
|
||
const req = require.context('./', true, /\.js$/); | ||
|
||
let modules = moduleTypes.reduce((memo, name) => { | ||
memo[camelCase(name)] = {}; | ||
return memo; | ||
}, {}); | ||
|
||
req.keys().forEach((modulePath) => { | ||
const moduleParts = modulePath.split('/'); | ||
const moduleType = moduleParts[1]; | ||
const moduleName = moduleParts[2]; | ||
|
||
if (moduleName) { | ||
const moduleKey = camelCase(moduleName.replace('.js', '')); | ||
modules[moduleType][moduleKey] = req(modulePath).default; | ||
} | ||
}); | ||
|
||
export function startDevMirage() { | ||
let options = Object.assign(modules, { baseConfig }); | ||
return new Mirage(options); | ||
} | ||
|
||
export function startTestMirage() { | ||
let testModules = { ...modules }; | ||
delete modules.scenarios; | ||
|
||
let options = Object.assign(testModules, { baseConfig }); | ||
return new Mirage(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,65 @@ | ||
import React from 'react'; | ||
import React, { Component } from 'react'; | ||
import fetch from 'isomorphic-fetch'; | ||
|
||
const App = () => ( | ||
<h1>Folio Resource Management</h1> | ||
) | ||
export default class App extends Component { | ||
constructor() { | ||
super(...arguments); | ||
|
||
App.displayName = 'App'; | ||
this.state = { | ||
search: '' | ||
}; | ||
} | ||
|
||
export default App; | ||
render () { | ||
let { | ||
search, | ||
searchResults | ||
} = this.state; | ||
|
||
return ( | ||
<div> | ||
<h1>Folio Resource Management</h1> | ||
<form onSubmit={this.searchSubmit}> | ||
<input | ||
type="search" | ||
name="search" | ||
value={search} | ||
placeholder="Search" | ||
data-test-search-field | ||
onChange={this.handleChange} /> | ||
<button data-test-search-submit type="submit" disabled={!search}>Search</button> | ||
</form> | ||
<ul data-test-search-results-list> | ||
{!!searchResults && searchResults.vendors.map((vendor) => ( | ||
<li data-test-search-results-item key={vendor.vendorId}> | ||
{vendor.vendorName} | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
get searchSubmit() { | ||
return (e) => { | ||
let { search } = this.state; | ||
|
||
e.preventDefault(); | ||
fetch(`/eholdings/vendors?search=${search}`) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
this.setState({ | ||
searchResults: data | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
get handleChange() { | ||
return (e) => { | ||
this.setState({ | ||
search: e.target.value | ||
}); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// This will force fetch to be polyfilled with XHR for pretender. | ||
// | ||
// We do this inside of another imported file becuase it needs to happen | ||
// before the fetch is first imported. Imports are hoisted to the top so | ||
// settings this explicity before importing the app still won't work; it | ||
// needs to be inside another import that happens before the app import. | ||
window.fetch = undefined; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const originalIt = window.it; | ||
|
||
// This turns every call of `it` into a "convergent assertion." The | ||
// assertion is run every 10ms until it is either true, or it times | ||
// out. This makes it incredibly robust in the face of asynchronous | ||
// operations which could happen instantly, or they could happen after | ||
// 1.5 seconds. The assertion doesn't care until it's reflected | ||
// in the UI. | ||
// | ||
// The only caveat is that all assertions should be "pure" that is to | ||
// say, completely without side-effects. | ||
// | ||
// good: | ||
// it('has some state', function() { | ||
// expect(thing).to.be('awesome'); | ||
// }); | ||
// | ||
// bad: | ||
// it('twiddles when clicked', function() { | ||
// click('.a-button'); | ||
// expect(thing).to.be('set'); | ||
// }); | ||
export default function itWill(...args) { | ||
if (args.length <= 1) { | ||
return originalIt(...args); | ||
} else { | ||
let [name, assertion] = args; | ||
|
||
return originalIt(name, function() { | ||
let timeout = this.timeout(); | ||
let interval = 10; | ||
let start = new Date().getTime(); | ||
let error = null; | ||
let test = this; | ||
|
||
return new Promise(function(resolve, reject) { | ||
(function loop() { | ||
try { | ||
let res = assertion.call(test); | ||
if (res && typeof res.then === 'function') { | ||
res.then(resolve); | ||
} else { | ||
resolve(); | ||
} | ||
} catch(e) { | ||
error = e; | ||
let now = new Date().getTime(); | ||
if (now - start + interval >= timeout) { | ||
reject(e); | ||
} else { | ||
setTimeout(loop, interval); | ||
} | ||
} | ||
})(); | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.