Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Polytech Todo Api Abouchtita
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BOUCHTITA Achraf
Polytech Todo Api Abouchtita
Commits
5c5cc39a
Commit
5c5cc39a
authored
3 months ago
by
abouchtita
Browse files
Options
Downloads
Patches
Plain Diff
Ajout de l'endpoint POST /api/tasks et de la fonction addTask
parent
ab758194
Branches
Branches containing commit
No related tags found
1 merge request
!1
Develop into main
Pipeline
#47826
failed
3 months ago
Stage: lint
Stage: test
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/index.js
+13
-0
13 additions, 0 deletions
src/index.js
src/taskService.js
+16
-1
16 additions, 1 deletion
src/taskService.js
tests/integration/app.test.js
+22
-0
22 additions, 0 deletions
tests/integration/app.test.js
tests/unit/todoService.test.js
+17
-2
17 additions, 2 deletions
tests/unit/todoService.test.js
with
68 additions
and
3 deletions
src/index.js
+
13
−
0
View file @
5c5cc39a
...
@@ -32,6 +32,19 @@ app.get('/api/tasks/:id', (req, res) => {
...
@@ -32,6 +32,19 @@ app.get('/api/tasks/:id', (req, res) => {
}
}
});
});
app
.
post
(
'
/api/tasks
'
,
(
req
,
res
)
=>
{
const
{
description
}
=
req
.
body
;
if
(
!
description
)
{
return
res
.
status
(
400
).
json
({
error
:
'
La description est requise
'
});
}
try
{
const
newTask
=
taskService
.
addTask
(
description
);
res
.
status
(
201
).
json
(
newTask
);
}
catch
(
error
)
{
res
.
status
(
500
).
json
({
error
:
error
.
message
});
}
});
// Fonction pour démarrer le serveur
// Fonction pour démarrer le serveur
const
startServer
=
()
=>
{
const
startServer
=
()
=>
{
const
server
=
app
.
listen
(
port
,
()
=>
{
const
server
=
app
.
listen
(
port
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
src/taskService.js
+
16
−
1
View file @
5c5cc39a
...
@@ -14,8 +14,23 @@ function getAllTasks() {
...
@@ -14,8 +14,23 @@ function getAllTasks() {
return
tasks
;
return
tasks
;
}
}
function
addTask
(
description
)
{
if
(
!
description
)
{
throw
new
Error
(
"
La description est requise
"
);
}
const
newId
=
tasks
.
reduce
((
max
,
task
)
=>
task
.
id
>
max
?
task
.
id
:
max
,
0
)
+
1
;
const
newTask
=
{
id
:
newId
,
description
:
description
,
done
:
false
};
tasks
.
push
(
newTask
);
return
newTask
;
}
//export des fonctions
//export des fonctions
module
.
exports
=
{
module
.
exports
=
{
getAllTasks
,
getAllTasks
,
getTaskById
getTaskById
,
addTask
};
};
This diff is collapsed.
Click to expand it.
tests/integration/app.test.js
+
22
−
0
View file @
5c5cc39a
...
@@ -43,3 +43,25 @@ describe('GET /api/tasks/:id', () => {
...
@@ -43,3 +43,25 @@ describe('GET /api/tasks/:id', () => {
});
});
});
});
describe
(
'
POST /api/tasks
'
,
()
=>
{
test
(
'
should create a new task and return it
'
,
async
()
=>
{
const
response
=
await
request
(
app
)
.
post
(
'
/api/tasks
'
)
.
send
({
description
:
"
Nouvelle tâche
"
});
expect
(
response
.
statusCode
).
toBe
(
201
);
expect
(
response
.
body
).
toHaveProperty
(
'
id
'
);
expect
(
response
.
body
.
description
).
toBe
(
"
Nouvelle tâche
"
);
expect
(
response
.
body
.
done
).
toBe
(
false
);
});
test
(
'
should return 400 if description is missing
'
,
async
()
=>
{
const
response
=
await
request
(
app
)
.
post
(
'
/api/tasks
'
)
.
send
({});
expect
(
response
.
statusCode
).
toBe
(
400
);
expect
(
response
.
body
).
toHaveProperty
(
'
error
'
,
'
La description est requise
'
);
});
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/unit/todoService.test.js
+
17
−
2
View file @
5c5cc39a
//test unitaire des fonctions de service
//test unitaire des fonctions de service
// Import de la fonction à tester
// Import de la fonction à tester
const
{
getAllTasks
,
getTaskById
}
=
require
(
'
../../src/taskService
'
);
const
{
getAllTasks
,
getTaskById
,
addTask
}
=
require
(
'
../../src/taskService
'
);
//Test de la fonction de récupération de toutes les tâches.
//Test de la fonction de récupération de toutes les tâches.
test
(
'
should return all tasks
'
,
()
=>
{
test
(
'
should return all tasks
'
,
()
=>
{
...
@@ -17,4 +17,19 @@ test('should return the correct task for a given id', () => {
...
@@ -17,4 +17,19 @@ test('should return the correct task for a given id', () => {
test
(
'
should return undefined for a non-existent task id
'
,
()
=>
{
test
(
'
should return undefined for a non-existent task id
'
,
()
=>
{
const
task
=
getTaskById
(
999
);
const
task
=
getTaskById
(
999
);
expect
(
task
).
toBeUndefined
();});
expect
(
task
).
toBeUndefined
();
\ No newline at end of file
});
test
(
'
should add a new task with provided description
'
,
()
=>
{
const
initialLength
=
getAllTasks
().
length
;
const
description
=
"
Nouvelle tâche
"
;
const
newTask
=
addTask
(
description
);
expect
(
newTask
).
toHaveProperty
(
'
id
'
);
expect
(
newTask
.
description
).
toBe
(
description
);
expect
(
newTask
.
done
).
toBe
(
false
);
expect
(
getAllTasks
().
length
).
toBe
(
initialLength
+
1
);
});
test
(
'
should throw an error if description is missing
'
,
()
=>
{
expect
(()
=>
addTask
()).
toThrow
(
"
La description est requise
"
);
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment