Skip to content

Commit

Permalink
feat(apiMock): default mock setting
Browse files Browse the repository at this point in the history
It should work for all commands, but tests only test for `true` for
now. Need to refactor the tests and code so we don’t need so much
repeat tests for the various commands.

Closes #24
  • Loading branch information
seriema committed Oct 3, 2015
1 parent 2156d54 commit f4e2258
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ Configure is done through `apiMockProvider.config()`. Add this to your AngularJS
});
````

#### defaultMock

Type: `boolean/string/number`

Default: `false`

Sets a default mock value. See [apiMock values](#apimock).

#### mockDataPath

Type: `string`
Expand Down
5 changes: 5 additions & 0 deletions app/scripts/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ angular.module('apiMock', [])
var $log;
var $q;
var config = {
defaultMock: false,
mockDataPath: '/mock_data',
apiPath: '/api',
disable: false,
Expand Down Expand Up @@ -163,9 +164,13 @@ angular.module('apiMock', [])

function getParameter(req) {
var mockValue = localMock(req);
// Note: `false` is a valid option, so we can't use falsy-checks.
if (mockValue === undefined) {
mockValue = globalMock();
}
if (mockValue === undefined) {
mockValue = config.defaultMock;
}

return mockValue;
}
Expand Down
63 changes: 59 additions & 4 deletions test/spec/services/angular-apimock.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('Service: apiMock', function () {
function expectHttpFailure(doneCb, failCb) {
$httpBackend.expect(defaultExpectMethod, defaultExpectPath).respond(404);

$http(defaultRequest)
$http(defaultRequest) // TODO: Callbacks isn't the proper way to test $http. It also doesn't seem to test properly as we can switch expectHttpSuccess() and expectHttpFailure() without tests failing.
.success(function () {
fail(); // Todo: How to fail the test if this happens?
fail();
failCb && failCb();
})
.error(function (data, status) {
Expand All @@ -107,7 +107,7 @@ describe('Service: apiMock', function () {

function expectHttpSuccess(doneCb, failCb) {
$httpBackend.expect(defaultExpectMethod, defaultExpectPath).respond({});
$http(defaultRequest)
$http(defaultRequest) // TODO: Callbacks isn't the proper way to test $http. It also doesn't seem to test properly as we can switch expectHttpSuccess() and expectHttpFailure() without tests failing.
.success(function () {
doneCb && doneCb();
})
Expand Down Expand Up @@ -381,7 +381,7 @@ describe('Service: apiMock', function () {

describe('off', function () {

it('should explicitly behave as usual with falsy values', function () {
it('should not mock with falsy values', function () {
// Define falsy values.
var values = [
false,
Expand Down Expand Up @@ -418,6 +418,16 @@ describe('Service: apiMock', function () {
apiMockProvider.config({disable: false});
});

it('should override config default mock', function () {
apiMockProvider.config({defaultMock: true});

// Test connection.
expectMockDisabled();
expectHttpFailure();

unsetGlobalCommand();
});

it('should override command mock', function () {
setGlobalCommand(true);

Expand All @@ -440,6 +450,51 @@ describe('Service: apiMock', function () {

});

describe('default mock option', function () {
beforeEach(function () {
//apiMockProvider.config({defaultMock: true});
});

afterEach(function () {
apiMockProvider.config({defaultMock: false});
unsetGlobalCommand();
});

it('should mock even without global or local flag', function () {
apiMockProvider.config({defaultMock: true});

// Test connection.
expectMockEnabled();
expectHttpSuccess();
});

it('should be overriden by global flag', function () {
apiMockProvider.config({defaultMock: true});
setGlobalCommand(false);

// Test connection.
expectMockDisabled();
expectHttpFailure();
});

it('should be overriden by local flag', function () {
apiMockProvider.config({defaultMock: true});
defaultRequest.apiMock = false;

// Test connection.
expectMockDisabled();
expectHttpFailure();
});

it('should not mock when set to false', function () {
apiMockProvider.config({defaultMock: false});

// Test connection.
expectMockDisabled();
expectHttpFailure();
});
});

describe('allow regexp for apiPath option instead of string', function () {

beforeEach(function () {
Expand Down

0 comments on commit f4e2258

Please sign in to comment.