All files / src/__tests__ fake-resource.ts

100% Statements 46/46
81.82% Branches 9/11
100% Functions 11/11
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136  1x 1x 1x 1x 1x 1x   23x     1x 1x 1x 1x 1x 1x     1x     1x                                                                                                   5x   5x 5x 10x 5x           5x 4x       1x 5x 5x 5x 5x               1x 1x 1x   1x 2x 1x             1x 1x         1x   1x                            
import Base, {ParamSet, ResourcePath} from '../base-rest-resource';
import {SessionInterface} from '../auth/session/types';
import {ApiVersion} from '../base-types';
 
interface FakeResourceFindArgs {
  params?: ParamSet;
  session: SessionInterface;
  id: number;
  other_resource_id?: number | null;
}
 
interface FakeResourceAllArgs {
  params?: ParamSet;
  session: SessionInterface;
}
 
interface FakeResourceCustomArgs {
  session: SessionInterface;
  id: number;
  other_resource_id: number;
}
 
export default class FakeResource extends Base {
  public static API_VERSION = ApiVersion.Unstable;
  protected static NAME = 'fake_resource';
  protected static PLURAL_NAME = 'fake_resources';
 
  protected static READ_ONLY_ATTRIBUTES: string[] = ['unsaveable_attribute'];
 
  protected static HAS_ONE = {
    has_one_attribute: FakeResource,
  };
 
  protected static HAS_MANY = {
    has_many_attribute: FakeResource,
  };
 
  protected static PATHS: ResourcePath[] = [
    {
      http_method: 'get',
      operation: 'get',
      ids: ['id'],
      path: 'fake_resources/<id>.json',
    },
    {
      http_method: 'get',
      operation: 'get',
      ids: [],
      path: 'fake_resources.json',
    },
    {
      http_method: 'post',
      operation: 'post',
      ids: [],
      path: 'fake_resources.json',
    },
    {
      http_method: 'put',
      operation: 'put',
      ids: ['id'],
      path: 'fake_resources/<id>.json',
    },
    {
      http_method: 'delete',
      operation: 'delete',
      ids: ['id'],
      path: 'fake_resources/<id>.json',
    },
    {
      http_method: 'get',
      operation: 'get',
      ids: ['id', 'other_resource_id'],
      path: 'other_resources/<other_resource_id>/fake_resources/<id>.json',
    },
    {
      http_method: 'get',
      operation: 'custom',
      ids: ['id', 'other_resource_id'],
      path: 'other_resources/<other_resource_id>/fake_resources/<id>/custom.json',
    },
    {
      http_method: 'delete',
      operation: 'delete',
      ids: ['id', 'other_resource_id'],
      path: 'other_resources/<other_resource_id>/fake_resources/<id>.json',
    },
  ];
 
  public static find = async ({
    session,
    params,
    id,
    other_resource_id = null,
    ...otherArgs
  }: FakeResourceFindArgs): Promise<FakeResource | null> => {
    const result = await FakeResource.baseFind({
      session,
      urlIds: {id, other_resource_id},
      params: {...params, ...otherArgs},
    });
    return result ? (result[0] as FakeResource) : null;
  };
 
  public static all = async ({
    session,
    params,
  }: FakeResourceAllArgs): Promise<FakeResource[]> => {
    return FakeResource.baseFind({
      session,
      params,
      urlIds: {},
    });
  };
 
  public static custom = async ({
    session,
    id,
    other_resource_id,
  }: FakeResourceCustomArgs): Promise<Body> => {
    const response = await FakeResource.request({
      http_method: 'get',
      operation: 'custom',
      session,
      urlIds: {id, other_resource_id},
    });
 
    return response.body as Body;
  };
 
  id?: number | string | null;
  attribute?: string | null;
  has_one_attribute?: FakeResource | null;
  has_many_attribute?: FakeResource[] | null;
  other_resource_id?: number | null;
}