123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
- var App = require('app');
- require('utils/configs_collection');
- describe('App.configsCollection', function () {
- var configsCollection;
- beforeEach(function () {
- configsCollection = Em.Object.create(App.configsCollection);
- sinon.spy(Em, 'assert');
- });
- afterEach(function () {
- Em.assert.restore();
- });
- describe('#add', function () {
- var throwCases = [
- {
- obj: undefined,
- collection: [],
- isError: true,
- title: 'null passed'
- },
- {
- obj: {},
- collection: [],
- isError: true,
- title: 'no id passed'
- },
- {
- obj: undefined,
- collection: [],
- isError: true,
- title: 'no item passed'
- }
- ];
- var cases = [
- {
- collection: [],
- isError: false,
- title: 'initial state'
- },
- {
- obj: {
- id: 1,
- name: 'n10'
- },
- collection: [
- {
- id: 1,
- name: 'n10'
- }
- ],
- mapItem: {
- id: 1,
- name: 'n10'
- },
- isError: false,
- title: 'new item'
- },
- {
- obj: {
- id: 1,
- name: 'n11'
- },
- collection: [
- {
- id: 1,
- name: 'n10'
- }
- ],
- mapItem: {
- id: 1,
- name: 'n11'
- },
- isError: false,
- title: 'duplicate id'
- },
- {
- obj: {
- id: '1',
- name: 'n12'
- },
- collection: [
- {
- id: 1,
- name: 'n10'
- }
- ],
- mapItem: {
- id: '1',
- name: 'n12'
- },
- isError: false,
- title: 'duplicate id, key name conversion'
- }
- ];
- throwCases.forEach(function (item) {
- it(item.title, function () {
- expect(function () {configsCollection.add(item.obj);}).to.throw(Error);
- });
- });
- cases.forEach(function (item) {
- describe(item.title, function () {
- beforeEach(function () {
- if (item.hasOwnProperty('obj')) {
- configsCollection.add(item.obj);
- }
- });
- it('configs array', function () {
- expect(configsCollection.getAll()).to.eql(item.collection);
- });
- if (item.obj && item.obj.id) {
- it('configs map', function () {
- expect(configsCollection.getConfig(item.obj.id)).to.eql(item.mapItem);
- });
- }
- });
- });
- });
- describe('#getConfig', function () {
- var throwCases = [
- {
- result: undefined,
- isError: true,
- title: 'no id passed'
- },
- {
- id: null,
- result: undefined,
- isError: true,
- title: 'invalid id passed'
- }
- ];
- var cases = [
- {
- id: 1,
- result: {
- id: 1
- },
- isError: false,
- title: 'existing item'
- },
- {
- id: 1,
- result: {
- id: 1
- },
- isError: false,
- title: 'existing item, key name conversion'
- },
- {
- id: 2,
- result: undefined,
- isError: false,
- title: 'item doesn\'t exist'
- }
- ];
- throwCases.forEach(function (item) {
- it(item.title, function () {
- configsCollection.add({
- id: 1
- });
- expect(function () {configsCollection.getConfig(item.id);}).to.throw(Error);
- });
- });
- cases.forEach(function (item) {
- describe(item.title, function () {
- var result;
- beforeEach(function () {
- configsCollection.add({
- id: 1
- });
- result = configsCollection.getConfig(item.id);
- });
- it('returned value', function () {
- expect(result).to.eql(item.result);
- });
- });
- });
- });
- describe('#getConfigByName', function () {
- var configIds = ['n0_f0', 'n1_f1'];
- var throwCases = [
- {
- fileName: 'f0',
- result: undefined,
- isError: true,
- title: 'no name passed'
- },
- {
- name: 'n0',
- result: undefined,
- isError: true,
- title: 'no filename passed'
- }
- ];
- var cases = [
- {
- name: 'n0',
- fileName: 'f0',
- result: {
- id: 'n0_f0'
- },
- isError: false,
- title: 'existing item'
- },
- {
- name: 'n0',
- fileName: 'f1',
- result: undefined,
- isError: false,
- title: 'not existing item'
- }
- ];
- beforeEach(function () {
- sinon.stub(App.config, 'configId', function (name, fileName) {
- return name + '_' + fileName;
- });
- });
- afterEach(function () {
- configsCollection.clearAll();
- App.config.configId.restore();
- });
- throwCases.forEach(function (item) {
- it(item.title, function () {
- expect(function () {configsCollection.getConfigByName(item.name, item.fileName);}).to.throw(Error);
- });
- });
- cases.forEach(function (item) {
- describe(item.title, function () {
- var result;
- beforeEach(function () {
- configIds.forEach(function (id) {
- configsCollection.add({
- id: id
- });
- });
- result = configsCollection.getConfigByName(item.name, item.fileName);
- });
- it('returned value', function () {
- expect(result).to.eql(item.result);
- });
- });
- });
- });
- describe('#getAll', function () {
- var configs = [
- {
- id: 'c0'
- },
- {
- id: 'c1'
- }
- ];
- beforeEach(function () {
- configsCollection.clearAll();
- });
- it('should return all configs', function () {
- configs.forEach(function (item) {
- configsCollection.add(item);
- });
- expect(configsCollection.getAll()).to.eql(configs);
- });
- });
- describe('#clearAll', function () {
- beforeEach(function () {
- configsCollection.add({
- id: 'c0'
- });
- configsCollection.clearAll();
- });
- it('should clear configs array', function () {
- expect(configsCollection.getAll()).to.have.length(0);
- });
- it('should clear configs map', function () {
- expect(configsCollection.getConfig('c0')).to.be.undefined;
- });
- });
- });
|