-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds response model with appropriate tests
- Loading branch information
1 parent
adc9b82
commit 891f793
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Response from './response'; | ||
|
||
it('will set the question identifier and response value', async () => { | ||
const response = new Response(); | ||
|
||
expect(response.for(1).is('the response')).toHaveProperty('attributes', { | ||
option: null, | ||
question: 1, | ||
value: 'the response', | ||
}); | ||
}); | ||
|
||
it('will set the question identifier and response option', async () => { | ||
const response = new Response(); | ||
|
||
expect(response.for(1).selected(2)).toHaveProperty('attributes', { | ||
option: 2, | ||
question: 1, | ||
value: null, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ModelInterface } from '../index'; | ||
import Model from './model'; | ||
|
||
export interface ResponseModel extends ModelInterface { | ||
for(question: number): this; | ||
|
||
is(value: string): this; | ||
|
||
selected(option: number): this; | ||
|
||
transform(): object; | ||
} | ||
|
||
export interface ResponseParameters { | ||
option: number | null; | ||
question: number | null; | ||
value: string | null; | ||
} | ||
|
||
export default class Response extends Model implements ResponseModel { | ||
protected attributes: ResponseParameters; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.attributes = { | ||
option: null, | ||
question: null, | ||
value: null, | ||
}; | ||
} | ||
|
||
public for(question: number): this { | ||
this.attributes.question = question; | ||
|
||
return this; | ||
} | ||
|
||
public is(value: string): this { | ||
this.attributes.value = value; | ||
|
||
return this; | ||
} | ||
|
||
public selected(option: number): this { | ||
this.attributes.option = option; | ||
|
||
return this; | ||
} | ||
|
||
public transform(): object { | ||
return { | ||
attributes: { | ||
form_option_id: this.attributes.option, | ||
form_question_id: this.attributes.question, | ||
value: this.attributes.value, | ||
}, | ||
type: 'responses', | ||
}; | ||
} | ||
} |