diff --git a/addon/utils/is-waypoint-record.js b/addon/utils/is-waypoint-record.js new file mode 100644 index 0000000..57d8025 --- /dev/null +++ b/addon/utils/is-waypoint-record.js @@ -0,0 +1,5 @@ +import WaypointModel from '../models/waypoint'; + +export default function isWaypointRecord(record) { + return record instanceof WaypointModel; +} diff --git a/app/utils/is-waypoint-record.js b/app/utils/is-waypoint-record.js new file mode 100644 index 0000000..65c9b8f --- /dev/null +++ b/app/utils/is-waypoint-record.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/fleetops-data/utils/is-waypoint-record'; diff --git a/tests/unit/utils/is-waypoint-record-test.js b/tests/unit/utils/is-waypoint-record-test.js new file mode 100644 index 0000000..3ab5538 --- /dev/null +++ b/tests/unit/utils/is-waypoint-record-test.js @@ -0,0 +1,47 @@ +import isWaypointRecord from 'dummy/utils/is-waypoint-record'; +import WaypointModel from 'dummy/models/waypoint'; +import PlaceModel from 'dummy/models/place'; +import { module, test } from 'qunit'; +import { setupTest } from 'dummy/tests/helpers'; +import ObjectProxy from '@ember/object/proxy'; + +module('Unit | Utility | is-waypoint-record', function (hooks) { + setupTest(hooks); + + hooks.beforeEach(function () { + this.store = this.owner.lookup('service:store'); + }); + + test('it returns true for a waypoint record', function (assert) { + assert.true(isWaypointRecord(this.store.createRecord('waypoint', {}))); + }); + + test('it returns false for a place, which waypoint extends', function (assert) { + // WaypointModel extends PlaceModel, so the check has to be narrow enough + // to reject the parent while still accepting the child. + assert.true(WaypointModel.prototype instanceof PlaceModel, 'waypoint does extend place'); + assert.false(isWaypointRecord(this.store.createRecord('place', {}))); + }); + + test('it returns false for an unrelated record', function (assert) { + assert.false(isWaypointRecord(this.store.createRecord('order', {}))); + }); + + test('it returns false for a proxy wrapping a waypoint', function (assert) { + const record = this.store.createRecord('waypoint', {}); + + assert.false(isWaypointRecord(ObjectProxy.create({ content: record })), 'instanceof looks at the proxy, not its content'); + }); + + test('it returns false for plain and nullish values', function (assert) { + assert.false(isWaypointRecord({})); + assert.false(isWaypointRecord(null)); + assert.false(isWaypointRecord(undefined)); + assert.false(isWaypointRecord('waypoint')); + assert.false(isWaypointRecord([])); + }); + + test('it returns false for an object merely shaped like a waypoint', function (assert) { + assert.false(isWaypointRecord({ place: {}, order: 1 }), 'the check is by identity, not by shape'); + }); +});