123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import Ember from 'ember';
- import DS from 'ember-data';
- import { module, test } from 'qunit';
- import startApp from '../helpers/start-app';
- var application, container, service;
- module('Acceptance: Ajax Service', {
- beforeEach: function() {
- application = startApp();
- container = application.__container__;
- service = container.lookup('service:ajax');
- },
- afterEach: function() {
- Ember.run(application, 'destroy');
- Ember.$.mockjax.clear();
- }
- });
- test('unpluralizeUrlProcedure fixes urls', function(assert) {
- assert.expect(3);
- var url = service.unpluralizeUrlProcedure('/some-words/', 'some-word');
- assert.equal(url, '/some-word/');
- url = service.unpluralizeUrlProcedure('/some-words/', 'someWord');
- assert.equal(url, '/some-word/');
- url = service.unpluralizeUrlProcedure('/model/some-words/', 'some-word');
- assert.equal(url, '/model/some-word/');
- });
- test('buildProcedureURL builds valid url', function(assert) {
- assert.expect(4);
- var adapter = container.lookup('adapter:application');
- var url = service.buildProcedureURL(adapter, 'close');
- assert.equal(url, '/api/close/');
- url = service.buildProcedureURL(adapter, 'close-thread');
- assert.equal(url, '/api/close-thread/');
- url = service.buildProcedureURL(adapter, 'closeThread');
- assert.equal(url, '/api/close-thread/');
- url = service.buildProcedureURL(adapter, 'thread/1/close');
- assert.equal(url, '/api/thread/1/close/');
- });
- test('successful get', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/some-value/',
- status: 200,
- responseText: {
- 'detail': 'it works'
- }
- });
- service.get('some-value').then(function(data) {
- assert.equal(data.detail, 'it works');
- }, function() {
- assert.ok(false, 'get call should pass');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- test('failed post', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/some-value/',
- status: 400,
- responseText: {
- 'detail': 'it fails'
- }
- });
- service.get('some-value').then(function(data) {
- assert.ok(false, 'get call should fail');
- }, function(jqXHR) {
- var rejection = jqXHR.responseJSON;
- assert.equal(rejection.detail, 'it fails');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- test('successful model prop get', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/',
- status: 200,
- responseText: {
- 'id': 'privacy-policy',
- 'title': '',
- 'link': '',
- 'body': ''
- }
- });
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/some-rpc/',
- status: 200,
- responseText: {
- 'detail': 'it works'
- }
- });
- Ember.run(function() {
- var store = container.lookup('store:main');
- store.find('legal-page', 'privacy-policy').then(function(record) {
- service.get(record, 'some-rpc').then(function(data) {
- assert.equal(data.detail, 'it works');
- }, function() {
- assert.ok(false, 'model get call should pass');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- });
- });
- test('failed model prop get', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/',
- status: 200,
- responseText: {
- 'id': 'privacy-policy',
- 'title': '',
- 'link': '',
- 'body': ''
- }
- });
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/some-rpc/',
- status: 400,
- responseText: {
- 'detail': 'it failed'
- }
- });
- Ember.run(function() {
- var store = container.lookup('store:main');
- store.find('legal-page', 'privacy-policy').then(function(record) {
- service.get(record, 'some-rpc').then(function() {
- assert.ok(false, 'get call should fail');
- }, function(jqXHR) {
- var rejection = jqXHR.responseJSON;
- assert.equal(rejection.detail, 'it failed');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- });
- });
- test('successful post', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/some-rpc/',
- status: 200,
- responseText: {
- 'detail': 'it works'
- }
- });
- service.post('some-rpc').then(function(data) {
- assert.equal(data.detail, 'it works');
- }, function() {
- assert.ok(false, 'rpc call should pass');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- test('failed post', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/some-rpc/',
- status: 400,
- responseText: {
- 'detail': 'it fails'
- }
- });
- service.post('some-rpc').then(function() {
- assert.ok(false, 'rpc call should fail');
- }, function(jqXHR) {
- var rejection = jqXHR.responseJSON;
- assert.equal(rejection.detail, 'it fails');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- test('successful model rpc post', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/',
- status: 200,
- responseText: {
- 'id': 'privacy-policy',
- 'title': '',
- 'link': '',
- 'body': ''
- }
- });
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/some-rpc/',
- status: 200,
- responseText: {
- 'detail': 'it works'
- }
- });
- Ember.run(function() {
- var store = container.lookup('store:main');
- store.find('legal-page', 'privacy-policy').then(function(record) {
- service.post(record, 'some-rpc').then(function(data) {
- assert.equal(data.detail, 'it works');
- }, function() {
- assert.ok(false, 'rpc call should pass');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- });
- });
- test('failed model rpc post', function(assert) {
- assert.expect(2);
- var done = assert.async();
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/',
- status: 200,
- responseText: {
- 'id': 'privacy-policy',
- 'title': '',
- 'link': '',
- 'body': ''
- }
- });
- Ember.$.mockjax({
- url: '/api/legal-pages/privacy-policy/some-rpc/',
- status: 400,
- responseText: {
- 'detail': 'it failed'
- }
- });
- Ember.run(function() {
- var store = container.lookup('store:main');
- store.find('legal-page', 'privacy-policy').then(function(record) {
- service.post(record, 'some-rpc').then(function() {
- assert.ok(false, 'rpc call should fail');
- }, function(jqXHR) {
- var rejection = jqXHR.responseJSON;
- assert.equal(rejection.detail, 'it failed');
- }).finally(function() {
- assert.ok(true, 'finally() was called');
- done();
- });
- });
- });
- });
|