From e4dbc78620a94d73d62c1c65d24d5729ac885fd5 Mon Sep 17 00:00:00 2001 From: Ava Gaiety Wroten Date: Fri, 27 Dec 2019 22:51:18 -0600 Subject: [PATCH] All basic tests passing --- ember-ui/app/components/recipe-list.hbs | 1 + ember-ui/app/components/recipe-preview.hbs | 10 ++-- ember-ui/app/models/meal.js | 2 +- ember-ui/mirage/config.js | 33 +++-------- ember-ui/mirage/factories/meal.js | 5 ++ .../integration/components/recipe-add-test.js | 7 ++- .../components/recipe-list-test.js | 22 +++----- .../components/recipe-preview-test.js | 56 ++++++++++++++----- ember-ui/tests/unit/models/meal-test.js | 19 ++++++- 9 files changed, 94 insertions(+), 61 deletions(-) create mode 100644 ember-ui/mirage/factories/meal.js diff --git a/ember-ui/app/components/recipe-list.hbs b/ember-ui/app/components/recipe-list.hbs index 0ebd96e..0c580cd 100644 --- a/ember-ui/app/components/recipe-list.hbs +++ b/ember-ui/app/components/recipe-list.hbs @@ -6,6 +6,7 @@ {{#each group.model as |meal|}} diff --git a/ember-ui/app/components/recipe-preview.hbs b/ember-ui/app/components/recipe-preview.hbs index c702750..faca852 100644 --- a/ember-ui/app/components/recipe-preview.hbs +++ b/ember-ui/app/components/recipe-preview.hbs @@ -1,18 +1,18 @@ - +
- +
-
+
{{@meal.name}}
-

+

{{@meal.ingredientsList}}

-
+
{{@meal.category}} {{@meal.area}}
diff --git a/ember-ui/app/models/meal.js b/ember-ui/app/models/meal.js index 42643c5..c486af6 100644 --- a/ember-ui/app/models/meal.js +++ b/ember-ui/app/models/meal.js @@ -6,13 +6,13 @@ export default class MealModel extends Model { @attr area; @attr category; @attr dateModified; + @attr ingredients; @attr instructions; @attr name; @attr sourceUrl; @attr tags; @attr thumbnailUrl; @attr youtubeUrl; - @attr ingredients; @attr('number', { defaultValue: Infinity }) listOrder; diff --git a/ember-ui/mirage/config.js b/ember-ui/mirage/config.js index 46b2015..3aadcd3 100644 --- a/ember-ui/mirage/config.js +++ b/ember-ui/mirage/config.js @@ -1,31 +1,12 @@ export default function() { - - // These comments are here to help you get started. Feel free to delete them. - - /* - Config (with defaults). - - Note: these only affect routes defined *after* them! - */ - - // this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server - // this.namespace = ''; // make this `/api`, for example, if your API is namespaced - // this.timing = 400; // delay for each request, automatically set to 0 during testing - - /* - Shorthand cheatsheet: - - this.get('/posts'); - this.post('/posts'); - this.get('/posts/:id'); - this.put('/posts/:id'); // or this.patch - this.del('/posts/:id'); - - https://www.ember-cli-mirage.com/docs/route-handlers/shorthands - */ this.urlPrefix = 'http://localhost:8000/'; - this.get('/meals/random', (schema, request) => { - return schema.meals.find(1); + this.get('/meals/random', (schema) => { + let id = 'random'; + schema.meals.create({id}); + return schema.meals.find(id); }); + + this.get('/meal/:id'); } + diff --git a/ember-ui/mirage/factories/meal.js b/ember-ui/mirage/factories/meal.js new file mode 100644 index 0000000..18efa08 --- /dev/null +++ b/ember-ui/mirage/factories/meal.js @@ -0,0 +1,5 @@ +import { Factory } from 'ember-cli-mirage'; + +export default Factory.extend({ + ingredients: [], +}); diff --git a/ember-ui/tests/integration/components/recipe-add-test.js b/ember-ui/tests/integration/components/recipe-add-test.js index 2d87344..27a4b4d 100644 --- a/ember-ui/tests/integration/components/recipe-add-test.js +++ b/ember-ui/tests/integration/components/recipe-add-test.js @@ -8,9 +8,14 @@ module('Integration | Component | recipe-add', function(hooks) { setupRenderingTest(hooks); setupMirage(hooks); - test('it renders', async function(assert) { + test('it adds a new recipe (meal) when button is clicked', async function(assert) { await render(hbs``); + let store = this.owner.lookup('service:store'); await click('[data-test-id=recipe-add-button]'); + let randomMeal = await store.findRecord('meal', 'random'); + + assert.ok(randomMeal); + assert.equal(store.peekAll('meal').length, 1); }); }); diff --git a/ember-ui/tests/integration/components/recipe-list-test.js b/ember-ui/tests/integration/components/recipe-list-test.js index fff2b41..c2ad487 100644 --- a/ember-ui/tests/integration/components/recipe-list-test.js +++ b/ember-ui/tests/integration/components/recipe-list-test.js @@ -1,26 +1,22 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; +import { setupMirage } from 'ember-cli-mirage/test-support'; import { render } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; module('Integration | Component | recipe-list', function(hooks) { setupRenderingTest(hooks); + setupMirage(hooks); - test('it renders', async function(assert) { - // Set any properties with this.set('myProperty', 'value'); - // Handle any actions with this.set('myAction', function(val) { ... }); + test('it renders existing meals into a list', async function(assert) { + let store = this.owner.lookup('service:store'); + this.server.create('meal', { id: 1 }); + this.server.create('meal', { id: 2 }); + await store.findRecord('meal', 1); + await store.findRecord('meal', 2); await render(hbs``); - assert.equal(this.element.textContent.trim(), ''); - - // Template block usage: - await render(hbs` - - template block text - - `); - - assert.equal(this.element.textContent.trim(), 'template block text'); + assert.dom('[data-test-id=recipe-item]').exists({ count: 2 }); }); }); diff --git a/ember-ui/tests/integration/components/recipe-preview-test.js b/ember-ui/tests/integration/components/recipe-preview-test.js index cc3c42a..f039475 100644 --- a/ember-ui/tests/integration/components/recipe-preview-test.js +++ b/ember-ui/tests/integration/components/recipe-preview-test.js @@ -1,26 +1,54 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; +import { setupMirage } from 'ember-cli-mirage/test-support'; import { render } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; module('Integration | Component | recipe-preview', function(hooks) { setupRenderingTest(hooks); + setupMirage(hooks); - test('it renders', async function(assert) { - // Set any properties with this.set('myProperty', 'value'); - // Handle any actions with this.set('myAction', function(val) { ... }); + hooks.beforeEach(initMeal); - await render(hbs``); + test('it renders a wrapper href', async function(assert) { + await render(hbs``); - assert.equal(this.element.textContent.trim(), ''); - - // Template block usage: - await render(hbs` - - template block text - - `); - - assert.equal(this.element.textContent.trim(), 'template block text'); + assert.dom('[data-test-id=recipe-link]').hasTagName('a'); }); + + test('it renders recipe preview image', async function(assert) { + await render(hbs``); + + assert.dom('[data-test-id=recipe-preview-image]').hasAttribute('src', 'image.jpg'); + }); + + test('it renders recipe data as text', async function(assert) { + await render(hbs``); + + assert.dom('[data-test-id=recipe-name]').hasText('Cookies'); + assert.dom('[data-test-id=recipe-ingredients-list]').hasText('Love, Chocolate'); + assert.dom('[data-test-id=recipe-tags]').includesText('Home'); + assert.dom('[data-test-id=recipe-tags]').includesText('Desserts'); + }); + + async function initMeal() { + let store = this.owner.lookup('service:store'); + this.server.create('meal', { + area: 'Home', + category: 'Desserts', + id: 1, + ingredients: [ + { + name: 'Love', + measure: 'Infinity', + }, { + name: 'Chocolate', + measure: 'As Much As You Desire', + }, + ], + name: 'Cookies', + thumbnailUrl: 'image.jpg', + }); + this.set('meal', await store.findRecord('meal', 1)); + } }); diff --git a/ember-ui/tests/unit/models/meal-test.js b/ember-ui/tests/unit/models/meal-test.js index c211490..6a3ede3 100644 --- a/ember-ui/tests/unit/models/meal-test.js +++ b/ember-ui/tests/unit/models/meal-test.js @@ -4,10 +4,27 @@ import { setupTest } from 'ember-qunit'; module('Unit | Model | meal', function(hooks) { setupTest(hooks); - // Replace this with your real tests. test('it exists', function(assert) { let store = this.owner.lookup('service:store'); let model = store.createRecord('meal', {}); + assert.ok(model); }); + + test('it creates comma separated ingredients list', function(assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('meal', { + ingredients: [ + { + name: 'Love', + measure: 'Infinity', + }, { + name: 'Chocolate', + measure: 'As Much As You Desire', + }, + ], + }); + + assert.equal(model.ingredientsList, 'Love, Chocolate'); + }); });