Skip to content

Commit

Permalink
Adds response model with appropriate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Jul 15, 2019
1 parent adc9b82 commit 891f793
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/models/response.test.ts
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,
});
});
61 changes: 61 additions & 0 deletions src/models/response.ts
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',
};
}
}

0 comments on commit 891f793

Please sign in to comment.