<commit_before>import { test } from 'substance-test'
import { INTERNAL_BIBR_TYPES } from '../index'
import setupTestApp from './shared/setupTestApp'
import { openMetadataEditor } from './shared/integrationTestHelpers'

test(`AddEntity: add author`, t => {
  _testAddEntity(t, 'author', 'person')
})

test(`AddEntity: add editor`, t => {
  _testAddEntity(t, 'editor', 'person')
})

test(`AddEntity: add group`, t => {
  _testAddEntity(t, 'group', 'group')
})

test(`AddEntity: add organisation`, t => {
  _testAddEntity(t, 'organisation', 'organisation')
})

test(`AddEntity: add award`, t => {
  _testAddEntity(t, 'award', 'award')
})

test(`AddEntity: add keyword`, t => {
  _testAddEntity(t, 'keyword', 'keyword')
})

test(`AddEntity: add subject`, t => {
  _testAddEntity(t, 'subject', 'subject')
})

test(`AddEntity: add footnote`, t => {
  _testAddEntity(t, 'footnote', 'fn')
})

// addding reference is done in a workflow, where the user can choose to import, or select a specific type
// TODO: we should also test the other ways to create reference (actually we should cover all cases)
// For now, I have added only the following tests for adding manually
INTERNAL_BIBR_TYPES.forEach(bibrType => {
  test(`AddEntity: add reference [type=${bibrType}]`, t => {
    _testAddReference(t, bibrType)
  })
})

function _testAddEntity (t, toolName, entityType) {
  let { app } = setupTestApp(t, { archiveId: 'blank' })
  let editor = openMetadataEditor(app)
  // NOTE: this will only fail in the nodejs test suite, because browser runs the click
  // in a try-catch block (or so)
  t.doesNotThrow(() => {
    _addEntity(editor, toolName)
  })
  t.notNil(editor.find(`.sc-card.sm-${entityType}`), 'there should be a card for the new entitiy')
  t.end()
}

function _testAddReference (t, bibrType) {
  let { app } = setupTestApp(t, { archiveId: 'blank' })
  let editor = openMetadataEditor(app)
  t.doesNotThrow(() => {
    let menu = editor.find('.sc-tool-dropdown.sm-add')
    menu.find('button').el.click()
    menu.find(`.sc-menu-item.sm-add-reference`).el.click()
    // ... this opens a modal
    editor.find(`.sc-modal-dialog .se-add-reference .se-type.sm-${bibrType}`).click()
  })
  t.notNil(editor.find(`.sc-card.sm-${bibrType}`), 'there should be a card for the new entitiy')
  t.end()
}

function _addEntity (editor, toolName) {
  // open the corresponding dropdown
  let menu = editor.find('.sc-tool-dropdown.sm-add')
  menu.find('button').el.click()
  let addButton = menu.find(`.sc-menu-item.sm-add-${toolName}`).el
  return addButton.click()
}<commit_msg>Extend AddEntity tests to check if remove+undo works too.<commit_after>import { test } from 'substance-test'
import { INTERNAL_BIBR_TYPES } from '../index'
import setupTestApp from './shared/setupTestApp'
import { openMetadataEditor } from './shared/integrationTestHelpers'

test(`AddEntity: add author`, t => {
  _testAddEntity(t, 'author', 'person')
})

test(`AddEntity: add editor`, t => {
  _testAddEntity(t, 'editor', 'person')
})

test(`AddEntity: add group`, t => {
  _testAddEntity(t, 'group', 'group')
})

test(`AddEntity: add organisation`, t => {
  _testAddEntity(t, 'organisation', 'organisation')
})

test(`AddEntity: add award`, t => {
  _testAddEntity(t, 'award', 'award')
})

test(`AddEntity: add keyword`, t => {
  _testAddEntity(t, 'keyword', 'keyword')
})

test(`AddEntity: add subject`, t => {
  _testAddEntity(t, 'subject', 'subject')
})

test(`AddEntity: add footnote`, t => {
  _testAddEntity(t, 'footnote', 'fn')
})

// addding reference is done in a workflow, where the user can choose to import, or select a specific type
// TODO: we should also test the other ways to create reference (actually we should cover all cases)
// For now, I have added only the following tests for adding manually
INTERNAL_BIBR_TYPES.forEach(bibrType => {
  test(`AddEntity: add reference [type=${bibrType}]`, t => {
    _testAddReference(t, bibrType)
  })
})

function _testAddEntity (t, toolName, entityType) {
  let { app } = setupTestApp(t, { archiveId: 'blank' })
  let editor = openMetadataEditor(app)
  // NOTE: this will only fail in the nodejs test suite, because browser runs the click
  // in a try-catch block (or so)
  t.doesNotThrow(() => {
    _addEntity(editor, toolName)
  })
  const cardSelector = `.sc-card.sm-${entityType}`
  let card = editor.find(cardSelector)
  t.notNil(card, 'there should be a card for the new entitiy')
  if (card) {
    // in addition to the plain 'Add Entity' we also test 'Remove+Undo'
    let modelId = card.el.getAttribute('data-id')
    editor.api.selectModel(modelId)
    t.doesNotThrow(() => {
      // remove the entity via remove button
      editor.find('.sc-toggle-tool.sm-remove-col-item > button').el.click()
    }, 'using "Remove" should not throw')
    t.nil(editor.find(cardSelector), 'card should have been removed')
    // now undo this change and then the card should be there again
    t.doesNotThrow(() => {
      editor.find('.sc-toggle-tool.sm-undo > button').el.click()
    }, 'using "Undo" should not throw')
    t.notNil(editor.find(cardSelector), 'card should be back again')
  }
  t.end()
}

function _testAddReference (t, bibrType) {
  let { app } = setupTestApp(t, { archiveId: 'blank' })
  let editor = openMetadataEditor(app)
  t.doesNotThrow(() => {
    let menu = editor.find('.sc-tool-dropdown.sm-add')
    menu.find('button').el.click()
    menu.find(`.sc-menu-item.sm-add-reference`).el.click()
    // ... this opens a modal
    editor.find(`.sc-modal-dialog .se-add-reference .se-type.sm-${bibrType}`).click()
  })
  t.notNil(editor.find(`.sc-card.sm-${bibrType}`), 'there should be a card for the new entitiy')
  t.end()
}

function _addEntity (editor, toolName) {
  // open the corresponding dropdown
  let menu = editor.find('.sc-tool-dropdown.sm-add')
  menu.find('button').el.click()
  let addButton = menu.find(`.sc-menu-item.sm-add-${toolName}`).el
  return addButton.click()
}
{'fraction_non_alphanumeric': 0.16214850985014312, 'fraction_numerical': 0.0, 'mean_word_length': 4.051020408163265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4893520', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1981, 'n_words': 535}