Skip to content

Commit

Permalink
Frontend Unit Tests(3): Cover multiple component files (oppia#13303)
Browse files Browse the repository at this point in the history
* intial commit

* minor fix
  • Loading branch information
gp201 authored Jul 7, 2021
1 parent 186435d commit e2e59bd
Show file tree
Hide file tree
Showing 11 changed files with 1,487 additions and 10 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Unti tests for the GraphInput response.
*/

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HtmlEscaperService } from 'services/html-escaper.service';
import { GraphDetailService } from './graph-detail.service';
import { ResponseGraphInput } from './oppia-response-graph-input.component';

describe('ResponseGraphInput', () => {
let component: ResponseGraphInput;
let fixture: ComponentFixture<ResponseGraphInput>;

let mockHtmlEscaperService = {
escapedJsonToObj: function(answer) {
return JSON.parse(answer);
}
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ResponseGraphInput],
providers: [GraphDetailService,
{
provide: HtmlEscaperService,
useValue: mockHtmlEscaperService
}]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ResponseGraphInput);
component = fixture.componentInstance;

component.answer = '{' +
' "isWeighted": false,' +
' "edges": [' +
' {' +
' "src": 0,' +
' "dst": 1,' +
' "weight": 1' +
' },' +
' {' +
' "src": 1,' +
' "dst": 2,' +
' "weight": 1' +
' }' +
' ],' +
' "isDirected": false,' +
' "vertices": [' +
' {' +
' "x": 150,' +
' "y": 50,' +
' "label": ""' +
' },' +
' {' +
' "x": 200,' +
' "y": 50,' +
' "label": ""' +
' },' +
' {' +
' "x": 150,' +
' "y": 100,' +
' "label": ""' +
' }' +
' ],' +
' "isLabeled": false' +
'}';

component.ngOnInit();
});

it('should create', () => {
component.ngOnInit();

expect(component.graph).toEqual({
isWeighted: false,
edges: [
{
src: 0,
dst: 1,
weight: 1
},
{
src: 1,
dst: 2,
weight: 1
}
],
isDirected: false,
vertices: [
{
x: 150,
y: 50,
label: ''
},
{
x: 200,
y: 50,
label: ''
},
{
x: 150,
y: 100,
label: ''
}
],
isLabeled: false
});
expect(component.VERTEX_RADIUS).toBe(6);
expect(component.EDGE_WIDTH).toBe(3);
});

it('should return directed edge arrow points when called', () => {
expect(component.getDirectedEdgeArrowPoints(0))
.toBe('196,50 186,45 186,55');
});

it('should return edge center when called', () => {
expect(component.getEdgeCenter(0)).toEqual({ x: 175, y: 50 });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Unit tests for the GraphInput short response.
*/

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { HtmlEscaperService } from 'services/html-escaper.service';
import { ShortResponseGraphInput } from './oppia-short-response-graph-input.component';

describe('ShortResponseGraphInput', () => {
let component: ShortResponseGraphInput;
let fixture: ComponentFixture<ShortResponseGraphInput>;

let mockHtmlEscaperService = {
escapedJsonToObj: function(answer) {
return JSON.parse(answer);
}
};

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot({
useDefaultLang: true,
isolate: false,
extend: false,
defaultLanguage: 'en'
})
],
declarations: [ShortResponseGraphInput],
providers: [
{
provide: HtmlEscaperService,
useValue: mockHtmlEscaperService
}
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ShortResponseGraphInput);
component = fixture.componentInstance;

component.answer = '{' +
' "isWeighted": false,' +
' "edges": [' +
' {' +
' "src": 0,' +
' "dst": 1,' +
' "weight": 1' +
' },' +
' {' +
' "src": 1,' +
' "dst": 2,' +
' "weight": 1' +
' }' +
' ],' +
' "isDirected": false,' +
' "vertices": [' +
' {' +
' "x": 150,' +
' "y": 50,' +
' "label": ""' +
' },' +
' {' +
' "x": 200,' +
' "y": 50,' +
' "label": ""' +
' },' +
' {' +
' "x": 150,' +
' "y": 100,' +
' "label": ""' +
' }' +
' ],' +
' "isLabeled": false' +
'}';
});

it('should create', () => {
component.ngOnInit();

expect(component.graph).toEqual({
isWeighted: false,
edges: [
{
src: 0,
dst: 1,
weight: 1
},
{
src: 1,
dst: 2,
weight: 1
}
],
isDirected: false,
vertices: [
{
x: 150,
y: 50,
label: ''
},
{
x: 200,
y: 50,
label: ''
},
{
x: 150,
y: 100,
label: ''
}
],
isLabeled: false
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Directive for the ImageClickInput response.
*/

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HtmlEscaperService } from 'services/html-escaper.service';
import { ResponseImageClickInput } from './oppia-response-image-click-input.component';

describe('ResponseImageClickInput', () => {
let component: ResponseImageClickInput;
let fixture: ComponentFixture<ResponseImageClickInput>;
let mockHtmlEscaperService = {
escapedJsonToObj: function(answer) {
return JSON.parse(answer);
}
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ResponseImageClickInput],
providers: [
{
provide: HtmlEscaperService,
useValue: mockHtmlEscaperService
}
]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ResponseImageClickInput);
component = fixture.componentInstance;

component.answer = '{' +
'"clickPosition": [' +
' 0.40913347791798105, ' +
' 0.39177101967799643 ' +
'],' +
'"clickedRegions": ["Region1"]' +
'}';
});

it('should initialise component when user submits answer', () => {
component.ngOnInit();

expect(component.clickRegionLabel).toBe('(Clicks on \'Region1\')');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Directive for the ImageClickInput response.
*/

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HtmlEscaperService } from 'services/html-escaper.service';
import { ShortResponseImageClickInput } from './oppia-short-response-image-click-input.component';

describe('ShortResponseImageClickInput', () => {
let component: ShortResponseImageClickInput;
let fixture: ComponentFixture<ShortResponseImageClickInput>;
let mockHtmlEscaperService = {
escapedJsonToObj: function(answer) {
return JSON.parse(answer);
}
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ShortResponseImageClickInput],
providers: [
{
provide: HtmlEscaperService,
useValue: mockHtmlEscaperService
}
]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ShortResponseImageClickInput);
component = fixture.componentInstance;

component.answer = '{' +
'"clickPosition": [' +
' 0.40913347791798105, ' +
' 0.39177101967799643 ' +
'],' +
'"clickedRegions": ["Region1"]' +
'}';
});

it('should initialise component when user submits answer', () => {
component.ngOnInit();

expect(component.clickRegionLabel).toBe('(Clicks on \'Region1\')');
});
});
Loading

0 comments on commit e2e59bd

Please sign in to comment.