Skip to content

Commit

Permalink
Added tests for localizable input directive dialog for saving & initi…
Browse files Browse the repository at this point in the history
…alisation
  • Loading branch information
zakhenry committed Oct 31, 2015
1 parent c0e0faa commit 1ca38ca
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 50 deletions.
@@ -1,19 +1,39 @@
namespace common.directives.localizableInput.dialog {

describe.skip('Localizable input dialog controller', () => {
describe.only('Localizable input dialog controller', () => {

let images:common.models.Image[] = common.models.ImageMock.collection(12),
$rootScope:global.IRootScope,
$scope:ng.IScope,
imageService:common.services.image.ImageService,
LocalizableInputDialogController:LocalizableInputDialogController,
$q:ng.IQService;
$q:ng.IQService,
mockInitLocalizations:common.models.Localization<common.models.Article>[] = [
common.models.LocalizationMock.entity({
localizations: {
title: "This is a title",
},
regionCode: 'uk',
}),
common.models.LocalizationMock.entity({
localizations: {
body: "This is the body",
},
regionCode: 'au',
}),
common.models.LocalizationMock.entity({
localizations: {
title: "Kiwi Title",
},
regionCode: 'nz',
})
];

beforeEach(() => {

module('app');

inject(($controller, _$rootScope_, _imageService_, _$q_) => {
inject(($controller, _$rootScope_, _imageService_, _$q_, _ngRestAdapter_) => {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();

Expand All @@ -27,73 +47,105 @@ namespace common.directives.localizableInput.dialog {
imageService.getPaginator = sinon.stub().returns(imagePaginatorMock);

LocalizableInputDialogController = $controller(common.directives.localizableInput.dialog.namespace + '.controller', {
localizations: mockInitLocalizations,
attributeKey: 'title',
inputNodeName: 'input',
originalValue: 'Original Title',
regionService: {
supportedRegions: [
{
code: 'au',
name: 'Australia',
},
{
code: 'uk',
name: 'United Kingdom',
},
{
code: 'nz',
name: 'New Zealand',
},
{
code: 'us',
name: 'USA',
},
{
code: 'fr',
name: 'France',
},
]
},
$mdDialog: {
cancel: sinon.stub(),
hide: sinon.stub()
},
imageService: imageService
notificationService: null,
ngRestAdapter: _ngRestAdapter_
});


$rootScope.$apply();

});

});

it('should be able to resolve image paginator with initial images', () => {


expect(LocalizableInputDialogController.library).to.have.length(12);
expect(LocalizableInputDialogController.library[0]).to.be.instanceOf(common.models.Image);

});

it('should be able to toggle selection of an image', () => {

LocalizableInputDialogController.selectedImage = null;

LocalizableInputDialogController.toggleImageSelection(LocalizableInputDialogController.library[2]);

expect(LocalizableInputDialogController.selectedImage).to.deep.equal(LocalizableInputDialogController.library[2]);

LocalizableInputDialogController.toggleImageSelection(LocalizableInputDialogController.library[2]);

expect(LocalizableInputDialogController.selectedImage).to.be.null;
it('should initialise a localizations map from source localizations', () => {

expect(LocalizableInputDialogController.localizationMap).to.have.property('uk', "This is a title");
expect(LocalizableInputDialogController.localizationMap).to.have.property('au', undefined);
expect(LocalizableInputDialogController.localizationMap).to.have.property('nz', "Kiwi Title");
expect(LocalizableInputDialogController.localizationMap).to.have.property('us', undefined);
expect(LocalizableInputDialogController.localizationMap).to.have.property('fr', undefined);
});

it('should be able to resolve a selected image', () => {

LocalizableInputDialogController.toggleImageSelection(LocalizableInputDialogController.library[0]);

LocalizableInputDialogController.selectImage();

expect((<any>LocalizableInputDialogController).$mdDialog.hide).to.have.been.calledWith(LocalizableInputDialogController.selectedImage);
it('should be able to copy the original value to a localization', () => {

});

it('should cancel the dialog when no image is selected', () => {
it('should be able to resolve the updated localizations', () => {

LocalizableInputDialogController.selectedImage = null;
LocalizableInputDialogController.localizationMap['au'] = "Aussie title";
LocalizableInputDialogController.localizationMap['nz'] = "";
LocalizableInputDialogController.localizationMap['us'] = "Murican title";

LocalizableInputDialogController.selectImage();

expect((<any>LocalizableInputDialogController).$mdDialog.cancel).to.have.been.called;
LocalizableInputDialogController.saveLocalizations();

expect((<any>LocalizableInputDialogController).$mdDialog.hide).to.have.been.calledWith([
{
localizableId: sinon.match.string,
localizableType: sinon.match.falsy,
localizations: {
body: "This is the body",
title: "Aussie title"
},
regionCode: 'au'
},
{
localizableId: sinon.match.string,
localizableType: sinon.match.falsy,
localizations: {
title: "This is a title"
},
regionCode: 'uk'
},
{
localizableId: sinon.match.string,
localizableType: sinon.match.falsy,
localizations: {
},
regionCode: 'nz'
},
{
localizableId: sinon.match.string,
localizableType: sinon.match.falsy,
localizations: {
title: "Murican title"
},
regionCode: 'us'
}
]);
});

it('should be able to browse through multiple pages of images', () => {

let pageChangePromise = LocalizableInputDialogController.goToPage(2);

expect(LocalizableInputDialogController.currentPage).to.equal(2);

$rootScope.$apply();

expect(pageChangePromise).eventually.to.deep.equal(images);

});

it('should be able to cancel the dialog', () => {

Expand All @@ -103,8 +155,6 @@ namespace common.directives.localizableInput.dialog {

});



});

}
Expand Up @@ -59,7 +59,7 @@ namespace common.directives.localizableInput.dialog {
let localization = _.find(this.localizations, {regionCode: regionCode});

if (!localization){
return null;
return undefined;
}

return localization.localizations[this.attributeKey];
Expand Down

0 comments on commit 1ca38ca

Please sign in to comment.