Skip to content

Commit

Permalink
Merge pull request #16 from WKLD-Labs/unit-test
Browse files Browse the repository at this point in the history
Unit test
  • Loading branch information
AdamRFaqih authored Jun 12, 2024
2 parents dc0fc03 + e4c89ae commit 8cbc322
Show file tree
Hide file tree
Showing 4 changed files with 4,742 additions and 3 deletions.
71 changes: 71 additions & 0 deletions app/__tests__/movie.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const request = require('supertest');
const app = require('../../server');
const db = require('../../app/models/index');

describe('CRUD Operations for /api/movies', () => {
let createdMovieId;

afterAll(async () => {
await db.sequelize.close();
});

// Test the Create (POST) operation
it('should create a new movie', async () => {
const newMovie = {
title: 'New Movie',
genre: 'Action',
director: 'John Doe',
year: 2021,
rating: 8
};

const res = await request(app)
.post('/api/movies')
.send(newMovie)
.expect('Content-Type', /json/)
.expect(201);

expect(res.body).toHaveProperty('movie');
expect(res.body.movie).toHaveProperty('id');
expect(res.body.movie.title).toEqual(newMovie.title);
createdMovieId = res.body.movie.id;
});

// Test the Read (GET) operation
it('should retrieve all movies', async () => {
const res = await request(app)
.get('/api/movies')
.expect('Content-Type', /json/)
.expect(200);

expect(Array.isArray(res.body)).toBe(true);
});

// Test the Read (GET) operation for a single movie
it('should retrieve a single movie', async () => {
const res = await request(app)
.get(`/api/movies/${createdMovieId}`)
.expect('Content-Type', /json/)
.expect(200);

expect(res.body).toHaveProperty('movie');
expect(res.body.movie.id).toEqual(createdMovieId);
});

// Test the Delete (DELETE) operation
it('should delete a movie', async () => {
const res = await request(app)
.delete(`/api/movies/${createdMovieId}`)
.expect('Content-Type', /json/);

// Check if the movie was deleted successfully
if (res.status === 200) {
expect(res.body).toHaveProperty('message', 'Movie deleted successfully');
} else if (res.status === 404) {
expect(res.body).toHaveProperty('message', 'Movie not found');
} else {
// Handle other error cases if needed
fail(`Unexpected response status: ${res.status}`);
}
});
});
Loading

0 comments on commit 8cbc322

Please sign in to comment.