 ]
 
 describe 'Rally.apps.teamboard.TeamBoardApp', ->
+
+  helpers
+    assertFieldsShownOnCard: (fieldNames) ->
+      cardEl = @cardboard().getColumns()[0].getCards()[0].getEl()
+
+      expect(cardEl.query('.field-content').length).toBe fieldNames.length
+
+      for fieldName in fieldNames
+        expect(cardEl.down('.field-content.' + fieldName)).not.toBeNull()
+
+    cardboard: ->
+      @app.down('.rallycardboard')
+
+    createApp: (options = {}) ->
+      @stubIsAdmin if options.isAdmin? then options.isAdmin else true
+      @stubProjectRecords options.projectRecords || @projectRecords
+
+      @app = Ext.create 'Rally.apps.teamboard.TeamBoardApp', Ext.apply(
+        context: Ext.create 'Rally.app.Context'
+        # renderTo: 'testDiv'
+      , options.appConfig)
+
+      if options.appConfig?.renderTo then @waitForComponentReady(@app) else webdriver.promise.fulfilled(@app)
+
+    stubIsAdmin: (isAdmin) ->
+      @stub(Rally.environment.getContext().getPermissions(), 'isWorkspaceOrSubscriptionAdmin').returns isAdmin
+
+    stubProjectRecords: (projectRecords) ->
+      @stub Rally.apps.teamboard.TeamBoardProjectRecordsLoader, 'load', (teamOids, callback, scope) ->
+        callback.call scope, projectRecords
+
   beforeEach ->
     @ajax.whenQuerying('user').respondWith @mom.getData('user')
 
     @projectRecords = @mom.getRecords 'project',
-      count: 15
+      count: 4
 
   afterEach ->
     @app?.destroy()

  it 'should not show card fields not visible to non-admins when user is a non-admin', ->
    @createApp(
      appConfig:
        settings:
          cardFields: 'EmailAddress,OnpremLdapUsername'
      isAdmin: false
    ).then =>
      @assertFieldsShownOnCard ['EmailAddress']

  helpers
    assertFieldsShownOnCard: (fieldNames) ->
      cardEl = @cardboard().getColumns()[0].getCards()[0].getEl()

      expect(cardEl.query('.field-content').length).toBe fieldNames.length

      for fieldName in fieldNames
        expect(cardEl.down('.field-content.' + fieldName)).not.toBeNull()

    cardboard: ->
      @app.down('.rallycardboard')

    createApp: (options = {}) ->
      @stubIsAdmin if options.isAdmin? then options.isAdmin else true
      @stubProjectRecords options.projectRecords || @projectRecords

      @app = Ext.create 'Rally.apps.teamboard.TeamBoardApp', Ext.apply(
        context: Ext.create 'Rally.app.Context'
        renderTo: 'testDiv'
      , options.appConfig)

      @waitForComponentReady @app

    stubIsAdmin: (isAdmin) ->
      @stub(Rally.environment.getContext().getPermissions(), 'isWorkspaceOrSubscriptionAdmin').returns isAdmin

    stubProjectRecords: (projectRecords) ->
      @stub Rally.apps.teamboard.TeamBoardProjectRecordsLoader, 'load', (teamOids, callback, scope) ->
        callback.call scope, projectRecords
  it 'should show a message when user does not have access to any of the teams chosen', ->
    @createApp(
      projectRecords: []
      appConfig:
        renderTo: 'testDiv'
    ).then =>
      expect(@app.getEl().down('.no-data')).not.toBe null

