Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Practice/005002 answer #2

Open
wants to merge 4 commits into
base: practice/005001_answer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions Jenkinsfile

This file was deleted.

17 changes: 17 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,21 @@ router.put('/api/task/:id', async function (req, res) {
res.json({task});
});

router.delete('/api/task/:id', async function (req, res) {
let id = req.params.id;


let task = await models.Task.findOne({
where: {
id
}
});

task = await task.destroy();

res.json({
task
});
});

module.exports = router;
38 changes: 37 additions & 1 deletion test/integration/user-creation-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var app = require('../../app');
var expect = require('expect.js');
var request = require('supertest');

describe.only('user creation page', function () {
describe('user creation page', function () {
before(async function () {
await require('../../models').sequelize.sync();
});
Expand Down Expand Up @@ -84,6 +84,42 @@ describe.only('user creation page', function () {
.and.to.have.property("completed");
expect(result.task.completed).to.equal(true);
});
it('透過 api 提供刪除 task 功能', async function () {
let username = 'frank test delete';
let user = await this.models.User.create({
username
});

let task = await this.models.Task.create({
title: 'frank test delete',
UserId: user.id,
completed: false
});

user = await this.models.User.findOne({
where: {username},
include: this.models.Task
});
expect(user.Tasks).to.be.an('array')
expect(user.Tasks.length).to.be.equal(1);

let response = await request(app)
.del(`/api/task/${task.id}`);
let result = response.body;

expect(result.task)
.to.be.an('object')
.and.to.have.property("title")
.and.to.have.property("completed");

user = await this.models.User.findOne({
where: {username},
include: this.models.Task
});

expect(user.Tasks).to.be.an('array')
expect(user.Tasks.length).to.be.equal(0);


});
});