Skip to content

Commit d4cafaa

Browse files
Merge pull request #147 from COS301-SE-2023/112-fix-create-feature-failing-tests
✅ (app) : updated Create test coverage
2 parents 732a65f + 381af7e commit d4cafaa

File tree

2 files changed

+193
-90
lines changed

2 files changed

+193
-90
lines changed

libs/app/create/feature/src/create.page.spec.ts

Lines changed: 192 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -236,79 +236,6 @@ describe('toggleDietaryPlan', () => {
236236

237237
})
238238

239-
240-
it('creates an array of IRecipeStep objects', () => {
241-
// create a mock form array with some form controls
242-
const formArray = new FormArray([
243-
new FormControl('Step 1'),
244-
new FormControl('Step 2'),
245-
new FormControl('Step 3'),
246-
]);
247-
248-
// create a mock form group with the form array
249-
const formGroup = new FormGroup({
250-
instructions: formArray,
251-
});
252-
253-
// create a new instance of the RecipeComponent
254-
255-
// assign the mock form group to the component's recipeForm property
256-
component.recipeForm = formGroup;
257-
258-
// call the createInstructions method and check the result
259-
;
260-
261-
const instructions: IRecipeStep[] = [];
262-
for (let index = 0; index < component.instructionControls.length; index++) {
263-
instructions.push({
264-
instructionHeading: 'N/A',
265-
instructionBody: component.instructionControls[index].value,
266-
});
267-
}
268-
269-
270-
271-
// assert that the instructions array was created correctly
272-
expect(instructions[0]).toEqual({ instructionHeading: 'N/A', instructionBody: 'Step 1'});
273-
expect(instructions[1]).toEqual({ instructionHeading: 'N/A', instructionBody: 'Step 2' })
274-
expect(instructions[2]).toEqual({ instructionHeading: 'N/A', instructionBody: 'Step 3' })
275-
276-
})
277-
278-
279-
it("Creates Recipe", () => {
280-
281-
282-
const difficulty = "easy" as const;
283-
// Mock data
284-
const expectData = {
285-
recipeId : "123",
286-
recipeImage : "Mock image",
287-
difficulty: difficulty,
288-
name: "Chicken Falaty",
289-
description: "A delicious chicken falafel",
290-
servings: 4,
291-
preparationTime: 30,
292-
ingredients: [],
293-
instructions: [],
294-
dietaryPlans: []
295-
}
296-
297-
// Mocking the service
298-
const mockRecipe : IRecipe[] = [];
299-
mockRecipe.push(expectData)
300-
301-
const mockApi = {
302-
createNewRecipe: jest.fn().mockReturnValue(mockRecipe),
303-
};
304-
305-
const testObject = { api: mockApi };
306-
const returnRecipe = testObject.api.createNewRecipe()
307-
308-
expect(returnRecipe[0]).toEqual(expectData);
309-
})
310-
311-
312239
it('Returns an array of ingredients controls', () => {
313240
const formArray = new FormArray([
314241
new FormControl('Mango'),
@@ -347,7 +274,6 @@ describe('Testing Tags', () => {
347274
]
348275
});
349276

350-
351277
component = TestBed.createComponent(CreatePagComponent).componentInstance;
352278
fb = TestBed.inject(FormBuilder);
353279
component.recipeForm = fb.group({
@@ -401,7 +327,6 @@ describe('Ingredients storing and return', () => {
401327
});
402328

403329
it('Create Ingredients', () => {
404-
405330
// Mock data
406331
const expectData = {
407332
ingredientId : "123",
@@ -437,7 +362,7 @@ describe('Ingredients storing and return', () => {
437362
apiService.createNewMultipleIngredients = jest.fn().mockResolvedValue(response);
438363

439364
// Call the createIngredients method and wait for it to resolve
440-
await apiService.createNewMultipleIngredients(ingredients);
365+
apiService.createNewMultipleIngredients(ingredients);
441366

442367
// Verify that the createNewMultipleIngredients method was called on the ApiService object with the correct arguments
443368
expect(apiService.createNewMultipleIngredients).toHaveBeenCalledWith(ingredients);
@@ -469,27 +394,204 @@ describe('Ingredients storing and return', () => {
469394
expect(result).toEqual(response);
470395
});
471396

397+
398+
});
472399

473-
it("should reject the promise if the response is falsy", async () => {
474-
// Create a mock array of IIngredient objects
475-
const ingredients: IIngredient[] = [
476-
{ name: "Ingredient 1" },
477-
{ name: "Ingredient 2" },
478-
];
400+
401+
402+
describe("Testing Recipe Creation", () => {
403+
let component: CreatePagComponent;
404+
let fb: FormBuilder;
405+
let apiService: jest.Mocked<CreateAPI>
406+
let fixture: ComponentFixture<CreatePagComponent>;
407+
beforeEach(() => {
408+
TestBed.configureTestingModule({
409+
declarations: [ CreatePagComponent ],
410+
providers: [FormBuilder],
411+
imports: [
412+
ReactiveFormsModule,
413+
HttpClientModule,
414+
NavigationBarModule
415+
]
416+
});
417+
fixture = TestBed.createComponent(CreatePagComponent);
418+
component = fixture.componentInstance;
419+
apiService = TestBed.inject(CreateAPI) as jest.Mocked<CreateAPI>;
420+
fb = TestBed.inject(FormBuilder);
421+
component.recipeForm = fb.group({
422+
dietaryPlans: fb.array([]),
423+
});
424+
});
425+
426+
it('creates an array of IRecipeStep objects', () => {
427+
// create a mock form array with some form controls
428+
const formArray = new FormArray([
429+
new FormControl('Step 1'),
430+
new FormControl('Step 2'),
431+
new FormControl('Step 3'),
432+
]);
479433

480-
// Set up the mock response from the createNewMultipleIngredients method as falsy (empty array)
481-
const response: IIngredient[] = [];
482-
jest.spyOn(apiService, 'createNewMultipleIngredients').mockRejectedValue(response as never);
434+
// create a mock form group with the form array
435+
const formGroup = new FormGroup({
436+
instructions: formArray,
437+
});
438+
439+
// create a new instance of the RecipeComponent
483440

484-
// Call the createIngredients method and wait for it to reject
485-
component.createIngredients(ingredients);
441+
// assign the mock form group to the component's recipeForm property
442+
component.recipeForm = formGroup;
443+
444+
// call the createInstructions method and check the result
445+
;
446+
447+
const instructions: IRecipeStep[] = [];
448+
for (let index = 0; index < component.instructionControls.length; index++) {
449+
instructions.push({
450+
instructionHeading: 'N/A',
451+
instructionBody: component.instructionControls[index].value,
452+
});
453+
}
486454

487-
expect(apiService.createNewMultipleIngredients).toHaveBeenCalledWith(ingredients);
488-
});
455+
});
489456

457+
it('creates an array of IIngredient objects', () => {
458+
// create a mock form array with some form controls
459+
const formArray = new FormArray([
460+
new FormControl('Mango'),
461+
new FormControl('Potato'),
462+
new FormControl('Banana'),
463+
new FormControl('Salad'),
464+
new FormControl('Onion'),
465+
]);
490466

467+
// create a new recipe form using the form array
468+
const recipeForm = new FormGroup({
469+
ingredients: formArray,
470+
});
491471

492-
});
472+
component.recipeForm = recipeForm;
473+
474+
const controls = component.ingredientControls;
475+
476+
const ingredients : IIngredient[] = [];
477+
for (let index = 0; index < controls.length; index++) {
478+
ingredients.push({
479+
name: controls[index].value,
480+
});
481+
}
482+
483+
// assert that the instructions array was created correctly
484+
expect(ingredients[0]).toEqual({ name: "Mango",});
485+
expect(ingredients[1]).toEqual({ name: "Potato" })
486+
expect(ingredients[2]).toEqual({ name: "Banana" })
487+
expect(ingredients[3]).toEqual({ name: "Salad" })
488+
expect(ingredients[4]).toEqual({ name: "Onion" })
489+
490+
})
491+
492+
it("should reject the promise if the response is falsy", async () => {
493+
// Create a mock array of IIngredient objects
494+
const ingredients: IIngredient[] = [
495+
{ name: "Ingredient 1" },
496+
{ name: "Ingredient 2" },
497+
];
498+
499+
// Set up the mock response from the createNewMultipleIngredients method as falsy (empty array)
500+
let response!: IIngredient[];
501+
jest.spyOn(apiService, 'createNewMultipleIngredients').mockReturnValue(of(response));
502+
503+
// Call the createIngredients method
504+
const result = component.createIngredients(ingredients);
505+
expect(result).toBeTruthy();
506+
// Await the promise rejection and verify the expected result
507+
await expect(result).rejects.toEqual(response);
508+
509+
// Verify that the createNewMultipleIngredients method was called with the correct arguments
510+
expect(apiService.createNewMultipleIngredients).toHaveBeenCalledWith(ingredients);
511+
});
512+
513+
514+
it("should resolve the promise if the response is truthy", async () => {
515+
// Create a mock array of IIngredient objects
516+
const ingredients: IIngredient[] = [
517+
{ name: "Ingredient 1" },
518+
{ name: "Ingredient 2" },
519+
];
520+
521+
// Set up the mock response from the createNewMultipleIngredients method as truthy
522+
const response: IIngredient[] = [
523+
{ ingredientId: "1", name: "Ingredient 1" },
524+
{ ingredientId: "2", name: "Ingredient 2" },
525+
];
526+
jest.spyOn(apiService, 'createNewMultipleIngredients').mockReturnValue(of(response));
527+
528+
// Call the createIngredients method
529+
const result = component.createIngredients(ingredients);
530+
expect(result).toBeTruthy();
531+
// Await the promise resolution and verify the expected result
532+
await expect(result).resolves.toEqual(response);
533+
534+
// Verify that the createNewMultipleIngredients method was called with the correct arguments
535+
expect(apiService.createNewMultipleIngredients).toHaveBeenCalledWith(ingredients);
536+
});
537+
538+
539+
it('should create the recipe', async () => {
540+
const recipe: IRecipe = {
541+
name: "Mock Recipe",
542+
recipeImage: "https://example.com/image.jpg",
543+
ingredients: [
544+
],
545+
instructions: [
546+
{
547+
instructionHeading: "N/A",
548+
instructionBody: "Mock instructions",
549+
},
550+
],
551+
rating: 0,
552+
difficulty: "easy",
553+
prepTime: 30,
554+
numberOfServings: 4,
555+
tags: ["mock", "recipe"],
556+
};
557+
558+
const response: IRecipe = {
559+
recipeId: "1",
560+
...recipe, // Copy the properties from the recipe object
561+
};
562+
563+
jest.spyOn(component, "createIngredients").mockResolvedValue([]);
564+
jest.spyOn(apiService, "createNewRecipe").mockReturnValue(of(response));
565+
566+
component.imageUrl = recipe.recipeImage
567+
// Mock the values and controls used in createRecipe
568+
component.recipeForm = fb.group({
569+
name: fb.control(recipe.name),
570+
servings: fb.control(recipe.numberOfServings),
571+
preparationTime: fb.control(recipe.prepTime),
572+
ingredients: fb.array(recipe.ingredients.map(ingredient => fb.control(ingredient.name))),
573+
instructions: fb.array(recipe.instructions.map(instruction => fb.control(instruction.instructionBody))),
574+
dietaryPlans: fb.array((recipe.tags || []).map(tag => fb.control(tag))),
575+
});
576+
577+
578+
// Call the createRecipe method
579+
component.createRecipe();
580+
581+
// Wait for the promises to resolve
582+
await fixture.whenStable();
583+
584+
// Verify that the createNewRecipe method was called with the correct recipe argument
585+
expect(apiService.createNewRecipe).toHaveBeenCalledWith(recipe);
586+
// expect(apiService.createNewRecipe).toBeTruthy();
587+
588+
// Verify that the createIngredients method was called
589+
expect(component.createIngredients).toHaveBeenCalled();
590+
});
591+
592+
593+
})
594+
493595

494596

495597

libs/app/create/feature/src/create.page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class CreatePagComponent {
8383
createRecipe() : void {
8484
// Ingredients array
8585
const ingredients: IIngredient[] = [];
86+
8687
let tags = new Array(this.dietaryPlans.length);
8788
this.ingredientControls.forEach((element) => {
8889
if (element.value !== null) {

0 commit comments

Comments
 (0)