Charles Peters UI Engineer

Mocking Data Quickly

November 29, 2017

Recently I've been needing to make a lot of demos really quickly. Every time I needed to mock out some UI components like a table view or a list view, data was really important. But Lorem ipsum is unpredicatable and most users would never input such shitty Latin. Cierco is never going to use your app, I'm sorry.

By the way, Luke Wroblewski cover this pretty vividly:

Using dummy content or fake information in the Web design process can result in products with unrealistic assumptions and potentially serious design flaws. A seemingly elegant design can quickly begin to bloat with unexpected content or break under the weight of actual activity. Fake data can ensure a nice looking layout but it doesn't reflect what a living, breathing application must endure. Real data does. -- Luke Wroblewski

I've typically stubbed out an object to match the design I needed, copy that same object a dozen times and there, you got yourself a poorly maintained fake array. This data is fake, now if you need to do something quick and dirty, we should aim for something that takes the shape of the data we need and looks similar to the API we need to work with later on. In other words, not as fake data.

I never want to create a manual stub ever again. It is so tedious. Manual stubs aren't effective because you'd need manually change something everywhere if you need tweak part of your object. Let's try and avoid that.

Randomly Creating Fake Data

So let's try something a little different to work with. Create an empty array.

const List =Array(50).fill()

There's a probably a better way to create a fake array but I can't think of one. I'm not on trial. Get off my back. I'm sorry writing code in public makes me tense...

Okay, I'm better now.

Now we need to create our fake schema. I'm going to use Chance.js, it's a great way to randomly produce predictable results we can start to work with.

I need to create a member list for a directory application in a coding interview a month ago and here's what I did:

We can use Chance to make a schema like object that's similar to the data we need to work with and not copied and pasted by hand. And when we need to change the shape we can change it in one place and have less to manage.

At the end we're mapping through the List Array we created because Array.map() returns another Array. Now we're passing a new instance of Chance to make sure each item in the new Array is unique.

Map through the results and get a new array of each of those empty spaces filled with your schema.

Bonus Points

Deploy this sucker! I've found that it's probably a good idea to not make the client-side do something the server will inevitably do, don't. It's not so hard to deploy a simple server with an instance of this faked data. Zeit's now (which I love and it's what this site is deployed on) makes it less burdensome to work with.