1
- import { PollLayoutType } from 'discord-api-types/v10' ;
1
+ import { PollLayoutType , type RESTAPIPoll } from 'discord-api-types/v10' ;
2
2
import { describe , test , expect } from 'vitest' ;
3
3
import { PollAnswerMediaBuilder , PollBuilder , PollQuestionBuilder } from '../../src/index.js' ;
4
4
@@ -7,22 +7,33 @@ const dummyData = {
7
7
text : '.' ,
8
8
} ,
9
9
answers : [ ] ,
10
- } ;
10
+ } satisfies RESTAPIPoll ;
11
+
12
+ const dummyDataWithAnswer = {
13
+ ...dummyData ,
14
+ answers : [
15
+ {
16
+ poll_media : {
17
+ text : '.' ,
18
+ } ,
19
+ } ,
20
+ ] ,
21
+ } satisfies RESTAPIPoll ;
11
22
12
23
describe ( 'Poll' , ( ) => {
13
24
describe ( 'Poll question' , ( ) => {
14
25
test ( 'GIVEN a poll with pre-defined question text THEN return valid toJSON data' , ( ) => {
15
- const poll = new PollBuilder ( { question : { text : 'foo' } } ) ;
26
+ const poll = new PollBuilder ( { ... dummyDataWithAnswer , question : { text : 'foo' } } ) ;
16
27
17
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { ...dummyData , question : { text : 'foo' } } ) ;
28
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { ...dummyDataWithAnswer , question : { text : 'foo' } } ) ;
18
29
} ) ;
19
30
20
31
test ( 'GIVEN a poll with question text THEN return valid toJSON data' , ( ) => {
21
- const poll = new PollBuilder ( ) ;
32
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
22
33
23
34
poll . setQuestion ( { text : 'foo' } ) ;
24
35
25
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { ...dummyData , question : { text : 'foo' } } ) ;
36
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { ...dummyDataWithAnswer , question : { text : 'foo' } } ) ;
26
37
} ) ;
27
38
28
39
test ( 'GIVEN a poll with invalid question THEN throws error' , ( ) => {
@@ -32,43 +43,43 @@ describe('Poll', () => {
32
43
33
44
describe ( 'Poll duration' , ( ) => {
34
45
test ( 'GIVEN a poll with pre-defined duration THEN return valid toJSON data' , ( ) => {
35
- const poll = new PollBuilder ( { duration : 1 , ...dummyData } ) ;
46
+ const poll = new PollBuilder ( { duration : 1 , ...dummyDataWithAnswer } ) ;
36
47
37
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { duration : 1 , ...dummyData } ) ;
48
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { duration : 1 , ...dummyDataWithAnswer } ) ;
38
49
} ) ;
39
50
40
51
test ( 'GIVEN a poll with duration THEN return valid toJSON data' , ( ) => {
41
- const poll = new PollBuilder ( dummyData ) ;
52
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
42
53
43
54
poll . setDuration ( 1 ) ;
44
55
45
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { duration : 1 , ...dummyData } ) ;
56
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { duration : 1 , ...dummyDataWithAnswer } ) ;
46
57
} ) ;
47
58
48
59
test ( 'GIVEN a poll with invalid duration THEN throws error' , ( ) => {
49
- const poll = new PollBuilder ( dummyData ) ;
60
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
50
61
51
62
expect ( ( ) => poll . setDuration ( 999 ) . toJSON ( ) ) . toThrowError ( ) ;
52
63
} ) ;
53
64
} ) ;
54
65
55
66
describe ( 'Poll layout type' , ( ) => {
56
67
test ( 'GIVEN a poll with pre-defined layout type THEN return valid toJSON data' , ( ) => {
57
- const poll = new PollBuilder ( { layout_type : PollLayoutType . Default , ... dummyData } ) ;
68
+ const poll = new PollBuilder ( { ... dummyDataWithAnswer , layout_type : PollLayoutType . Default } ) ;
58
69
59
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { layout_type : PollLayoutType . Default , ...dummyData } ) ;
70
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { layout_type : PollLayoutType . Default , ...dummyDataWithAnswer } ) ;
60
71
} ) ;
61
72
62
73
test ( 'GIVEN a poll with layout type THEN return valid toJSON data' , ( ) => {
63
- const poll = new PollBuilder ( dummyData ) ;
74
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
64
75
65
76
poll . setLayoutType ( PollLayoutType . Default ) ;
66
77
67
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { layout_type : PollLayoutType . Default , ...dummyData } ) ;
78
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { layout_type : PollLayoutType . Default , ...dummyDataWithAnswer } ) ;
68
79
} ) ;
69
80
70
81
test ( 'GIVEN a poll with invalid layout type THEN throws error' , ( ) => {
71
- const poll = new PollBuilder ( dummyData ) ;
82
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
72
83
73
84
// @ts -expect-error Invalid layout type
74
85
expect ( ( ) => poll . setLayoutType ( - 1 ) . toJSON ( ) ) . toThrowError ( ) ;
@@ -77,28 +88,34 @@ describe('Poll', () => {
77
88
78
89
describe ( 'Poll multi select' , ( ) => {
79
90
test ( 'GIVEN a poll with pre-defined multi select enabled THEN return valid toJSON data' , ( ) => {
80
- const poll = new PollBuilder ( { allow_multiselect : true , ...dummyData } ) ;
91
+ const poll = new PollBuilder ( { allow_multiselect : true , ...dummyDataWithAnswer } ) ;
81
92
82
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { allow_multiselect : true , ...dummyData } ) ;
93
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { allow_multiselect : true , ...dummyDataWithAnswer } ) ;
83
94
} ) ;
84
95
85
96
test ( 'GIVEN a poll with multi select enabled THEN return valid toJSON data' , ( ) => {
86
- const poll = new PollBuilder ( dummyData ) ;
97
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
87
98
88
99
poll . setMultiSelect ( ) ;
89
100
90
- expect ( poll . toJSON ( ) ) . toStrictEqual ( { allow_multiselect : true , ...dummyData } ) ;
101
+ expect ( poll . toJSON ( ) ) . toStrictEqual ( { allow_multiselect : true , ...dummyDataWithAnswer } ) ;
91
102
} ) ;
92
103
93
104
test ( 'GIVEN a poll with invalid multi select value THEN throws error' , ( ) => {
94
- const poll = new PollBuilder ( dummyData ) ;
105
+ const poll = new PollBuilder ( dummyDataWithAnswer ) ;
95
106
96
107
// @ts -expect-error Invalid multi-select value
97
108
expect ( ( ) => poll . setMultiSelect ( 'string' ) . toJSON ( ) ) . toThrowError ( ) ;
98
109
} ) ;
99
110
} ) ;
100
111
101
112
describe ( 'Poll answers' , ( ) => {
113
+ test ( 'GIVEN a poll without answers THEN throws error' , ( ) => {
114
+ const poll = new PollBuilder ( dummyData ) ;
115
+
116
+ expect ( ( ) => poll . toJSON ( ) ) . toThrowError ( ) ;
117
+ } ) ;
118
+
102
119
test ( 'GIVEN a poll with pre-defined answer THEN returns valid toJSON data' , ( ) => {
103
120
const poll = new PollBuilder ( {
104
121
...dummyData ,
0 commit comments